added the flower generator

This commit is contained in:
Ellpeck 2018-11-04 16:38:09 +01:00
parent 3ea561ec3e
commit b90b5c9f8c
14 changed files with 319 additions and 52 deletions

View file

@ -8,22 +8,22 @@ import de.ellpeck.naturesaura.reg.ModRegistry;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Map;
public class BlockContainerImpl extends BlockContainer implements IModItem, IModelProvider {
@ -85,19 +85,36 @@ public class BlockContainerImpl extends BlockContainer implements IModItem, IMod
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
if (!worldIn.isRemote) {
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityImpl) {
IItemHandler handler = ((TileEntityImpl) tile).getItemHandler(null);
if (handler != null) {
for (int i = 0; i < handler.getSlots(); i++) {
ItemStack stack = handler.getStackInSlot(i);
if (!stack.isEmpty()) {
EntityItem item = new EntityItem(worldIn, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, stack);
worldIn.spawnEntity(item);
}
}
}
}
if (tile instanceof TileEntityImpl)
((TileEntityImpl) tile).dropInventory();
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileEntityImpl)
drops.add(((TileEntityImpl) tile).getDrop(state, fortune));
else
super.getDrops(drops, world, pos, state, fortune);
}
@Override
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest) {
return willHarvest || super.removedByPlayer(state, world, pos, player, false);
}
@Override
public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack) {
super.harvestBlock(worldIn, player, pos, state, te, stack);
worldIn.setBlockToAir(pos);
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityImpl)
((TileEntityImpl) tile).loadDataOnPlace(stack);
}
}

View file

@ -0,0 +1,13 @@
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.blocks.tiles.TileEntityFlowerGenerator;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
public class BlockFlowerGenerator extends BlockContainerImpl {
public BlockFlowerGenerator() {
super(Material.WOOD, "flower_generator", TileEntityFlowerGenerator.class, "flower_generator");
this.setSoundType(SoundType.WOOD);
this.setHardness(2F);
}
}

View file

@ -28,4 +28,5 @@ public final class ModBlocks {
public static final Block POTION_GENERATOR = new BlockPotionGenerator();
public static final Block AURA_DETECTOR = new BlockAuraDetector();
public static final Block CONVERSION_CATALYST = new BlockImpl("conversion_catalyst", Material.ROCK).setSoundType(SoundType.STONE).setHardness(2.5F);
public static final Block FLOWER_GENERATOR = new BlockFlowerGenerator();
}

View file

@ -29,14 +29,16 @@ public class TileEntityAncientLeaves extends TileEntityImpl {
}
@Override
public void writeNBT(NBTTagCompound compound, boolean syncing) {
super.writeNBT(compound, syncing);
this.container.writeNBT(compound);
public void writeNBT(NBTTagCompound compound, SaveType type) {
super.writeNBT(compound, type);
if (type != SaveType.BLOCK)
this.container.writeNBT(compound);
}
@Override
public void readNBT(NBTTagCompound compound, boolean syncing) {
super.readNBT(compound, syncing);
this.container.readNBT(compound);
public void readNBT(NBTTagCompound compound, SaveType type) {
super.readNBT(compound, type);
if (type != SaveType.BLOCK)
this.container.readNBT(compound);
}
}

View file

@ -0,0 +1,137 @@
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.aura.chunk.AuraChunk;
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticleStream;
import de.ellpeck.naturesaura.packet.PacketParticles;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFlower;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.ITickable;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import org.apache.commons.lang3.mutable.MutableInt;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TileEntityFlowerGenerator extends TileEntityImpl implements ITickable {
private static final List<IBlockState> FLOWERS = new ArrayList<>();
static {
for (Block block : ForgeRegistries.BLOCKS) {
if (block instanceof BlockFlower) {
FLOWERS.addAll(block.getBlockState().getValidStates());
}
}
}
private final Map<IBlockState, MutableInt> consumedRecently = new HashMap<>();
@Override
public void update() {
if (!this.world.isRemote && this.world.getTotalWorldTime() % 10 == 0) {
List<BlockPos> possible = new ArrayList<>();
int range = 3;
for (int x = -range; x <= range; x++) {
for (int z = -range; z <= range; z++) {
BlockPos offset = this.pos.add(x, 0, z);
IBlockState state = this.world.getBlockState(offset);
if (FLOWERS.contains(state)) {
possible.add(offset);
}
}
}
if (possible.isEmpty())
return;
BlockPos pos = possible.get(this.world.rand.nextInt(possible.size()));
IBlockState state = this.world.getBlockState(pos);
MutableInt curr = this.consumedRecently.computeIfAbsent(state, s -> new MutableInt());
int toAdd = Math.max(0, 50 - curr.getValue());
if (toAdd > 0) {
BlockPos auraPos = AuraChunk.getLowestSpot(this.world, this.pos, 30, this.pos);
AuraChunk auraChunk = AuraChunk.getAuraChunk(this.world, auraPos);
auraChunk.storeAura(auraPos, toAdd);
}
for (Map.Entry<IBlockState, MutableInt> entry : this.consumedRecently.entrySet()) {
if (entry.getKey() != state) {
MutableInt val = entry.getValue();
if (val.getValue() > 0)
val.subtract(1);
}
}
curr.add(5);
this.world.setBlockToAir(pos);
int color = Helper.blendColors(0x5ccc30, 0xe53c16, toAdd / 50F);
if (toAdd > 0) {
for (int i = this.world.rand.nextInt(5) + 5; i >= 0; i--)
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticleStream(
pos.getX() + 0.25F + this.world.rand.nextFloat() * 0.5F,
pos.getY() + 0.25F + this.world.rand.nextFloat() * 0.5F,
pos.getZ() + 0.25F + this.world.rand.nextFloat() * 0.5F,
this.pos.getX() + 0.25F + this.world.rand.nextFloat() * 0.5F,
this.pos.getY() + 0.25F + this.world.rand.nextFloat() * 0.5F,
this.pos.getZ() + 0.25F + this.world.rand.nextFloat() * 0.5F,
this.world.rand.nextFloat() * 0.01F + 0.05F, color, 1F
));
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(this.pos.getX(), this.pos.getY(), this.pos.getZ(), 8));
}
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(pos.getX(), pos.getY(), pos.getZ(), 7, color));
}
}
@Override
public void writeNBT(NBTTagCompound compound, SaveType type) {
super.writeNBT(compound, type);
if (type != SaveType.SYNC && !this.consumedRecently.isEmpty()) {
NBTTagList list = new NBTTagList();
for (Map.Entry<IBlockState, MutableInt> entry : this.consumedRecently.entrySet()) {
IBlockState state = entry.getKey();
Block block = state.getBlock();
NBTTagCompound tag = new NBTTagCompound();
tag.setString("block", block.getRegistryName().toString());
tag.setInteger("meta", block.getMetaFromState(state));
tag.setInteger("amount", entry.getValue().intValue());
list.appendTag(tag);
}
compound.setTag("consumed_recently", list);
}
}
@Override
public void readNBT(NBTTagCompound compound, SaveType type) {
super.readNBT(compound, type);
if (type != SaveType.SYNC) {
this.consumedRecently.clear();
NBTTagList list = compound.getTagList("consumed_recently", 10);
for (NBTBase base : list) {
NBTTagCompound tag = (NBTTagCompound) base;
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(tag.getString("block")));
if (block != null) {
IBlockState state = block.getStateFromMeta(tag.getInteger("meta"));
this.consumedRecently.put(state, new MutableInt(tag.getInteger("amount")));
}
}
}
}
}

View file

@ -51,7 +51,7 @@ public class TileEntityFurnaceHeater extends TileEntityImpl implements ITickable
}
}
if(did != this.isActive){
if (did != this.isActive) {
this.isActive = did;
this.sendToClients();
}
@ -75,18 +75,18 @@ public class TileEntityFurnaceHeater extends TileEntityImpl implements ITickable
}
@Override
public void writeNBT(NBTTagCompound compound, boolean syncing) {
super.writeNBT(compound, syncing);
public void writeNBT(NBTTagCompound compound, SaveType type) {
super.writeNBT(compound, type);
if (syncing)
if (type == SaveType.SYNC)
compound.setBoolean("active", this.isActive);
}
@Override
public void readNBT(NBTTagCompound compound, boolean syncing) {
super.readNBT(compound, syncing);
public void readNBT(NBTTagCompound compound, SaveType type) {
super.readNBT(compound, type);
if (syncing)
if (type == SaveType.SYNC)
this.isActive = compound.getBoolean("active");
}
}

View file

@ -2,7 +2,10 @@ package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.aura.Capabilities;
import de.ellpeck.naturesaura.aura.container.IAuraContainer;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
@ -14,6 +17,7 @@ import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import javax.annotation.Nullable;
@ -26,46 +30,48 @@ public class TileEntityImpl extends TileEntity {
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
this.writeNBT(compound, false);
this.writeNBT(compound, SaveType.TILE);
return compound;
}
@Override
public void readFromNBT(NBTTagCompound compound) {
this.readNBT(compound, false);
this.readNBT(compound, SaveType.TILE);
}
public void writeNBT(NBTTagCompound compound, boolean syncing) {
super.writeToNBT(compound);
public void writeNBT(NBTTagCompound compound, SaveType type) {
if (type != SaveType.BLOCK)
super.writeToNBT(compound);
}
public void readNBT(NBTTagCompound compound, boolean syncing) {
super.readFromNBT(compound);
public void readNBT(NBTTagCompound compound, SaveType type) {
if (type != SaveType.BLOCK)
super.readFromNBT(compound);
}
@Override
public final SPacketUpdateTileEntity getUpdatePacket() {
NBTTagCompound compound = new NBTTagCompound();
this.writeNBT(compound, true);
this.writeNBT(compound, SaveType.SYNC);
return new SPacketUpdateTileEntity(this.pos, 0, compound);
}
@Override
public final NBTTagCompound getUpdateTag() {
NBTTagCompound compound = new NBTTagCompound();
this.writeNBT(compound, true);
this.writeNBT(compound, SaveType.SYNC);
return compound;
}
@Override
public void handleUpdateTag(NBTTagCompound tag) {
this.readNBT(tag, true);
this.readNBT(tag, SaveType.SYNC);
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
super.onDataPacket(net, packet);
this.readNBT(packet.getNbtCompound(), true);
this.readNBT(packet.getNbtCompound(), SaveType.SYNC);
}
public void sendToClients() {
@ -106,4 +112,51 @@ public class TileEntityImpl extends TileEntity {
return super.getCapability(capability, facing);
}
}
public void dropInventory() {
IItemHandler handler = this.getItemHandler(null);
if (handler != null) {
for (int i = 0; i < handler.getSlots(); i++) {
ItemStack stack = handler.getStackInSlot(i);
if (!stack.isEmpty()) {
EntityItem item = new EntityItem(this.world,
this.pos.getX() + 0.5, this.pos.getY() + 0.5, this.pos.getZ() + 0.5,
stack);
this.world.spawnEntity(item);
}
}
}
}
public ItemStack getDrop(IBlockState state, int fortune) {
Block block = state.getBlock();
ItemStack stack = new ItemStack(
block.getItemDropped(state, this.world.rand, fortune),
block.quantityDropped(state, fortune, this.world.rand),
block.damageDropped(state));
NBTTagCompound compound = new NBTTagCompound();
this.writeNBT(compound, SaveType.BLOCK);
if (!compound.isEmpty()) {
stack.setTagCompound(new NBTTagCompound());
stack.getTagCompound().setTag("data", compound);
}
return stack;
}
public void loadDataOnPlace(ItemStack stack) {
if (stack.hasTagCompound()) {
NBTTagCompound compound = stack.getTagCompound().getCompoundTag("data");
if (compound != null)
this.readNBT(compound, SaveType.BLOCK);
}
}
public enum SaveType {
TILE,
SYNC,
BLOCK
}
}

View file

@ -40,7 +40,7 @@ public class TileEntityNatureAltar extends TileEntityImpl implements ITickable {
@Override
protected boolean canExtract(ItemStack stack, int slot, int amount) {
if(stack.hasCapability(Capabilities.auraContainer, null))
if (stack.hasCapability(Capabilities.auraContainer, null))
return stack.getCapability(Capabilities.auraContainer, null).storeAura(1, true) <= 0;
else
return AltarRecipe.forInput(stack) == null;
@ -186,13 +186,13 @@ public class TileEntityNatureAltar extends TileEntityImpl implements ITickable {
}
@Override
public void writeNBT(NBTTagCompound compound, boolean syncing) {
super.writeNBT(compound, syncing);
public void writeNBT(NBTTagCompound compound, SaveType type) {
super.writeNBT(compound, type);
compound.setTag("items", this.items.serializeNBT());
compound.setBoolean("fine", this.structureFine);
this.container.writeNBT(compound);
if (!syncing) {
if (type == SaveType.TILE) {
if (this.currentRecipe != null) {
compound.setString("recipe", this.currentRecipe.name.toString());
compound.setInteger("timer", this.timer);
@ -201,13 +201,13 @@ public class TileEntityNatureAltar extends TileEntityImpl implements ITickable {
}
@Override
public void readNBT(NBTTagCompound compound, boolean syncing) {
super.readNBT(compound, syncing);
public void readNBT(NBTTagCompound compound, SaveType type) {
super.readNBT(compound, type);
this.items.deserializeNBT(compound.getCompoundTag("items"));
this.structureFine = compound.getBoolean("fine");
this.container.readNBT(compound);
if (!syncing) {
if (type == SaveType.TILE) {
if (compound.hasKey("recipe")) {
this.currentRecipe = AltarRecipe.RECIPES.get(new ResourceLocation(compound.getString("recipe")));
this.timer = compound.getInteger("timer");

View file

@ -163,11 +163,11 @@ public class TileEntityWoodStand extends TileEntityImpl implements ITickable {
}
@Override
public void writeNBT(NBTTagCompound compound, boolean syncing) {
super.writeNBT(compound, syncing);
public void writeNBT(NBTTagCompound compound, SaveType type) {
super.writeNBT(compound, type);
compound.setTag("items", this.items.serializeNBT());
if (!syncing) {
if (type == SaveType.TILE) {
if (this.ritualPos != null && this.recipe != null) {
compound.setLong("ritual_pos", this.ritualPos.toLong());
compound.setInteger("timer", this.timer);
@ -177,11 +177,11 @@ public class TileEntityWoodStand extends TileEntityImpl implements ITickable {
}
@Override
public void readNBT(NBTTagCompound compound, boolean syncing) {
super.readNBT(compound, syncing);
public void readNBT(NBTTagCompound compound, SaveType type) {
super.readNBT(compound, type);
this.items.deserializeNBT(compound.getCompoundTag("items"));
if (!syncing) {
if (type == SaveType.TILE) {
if (compound.hasKey("recipe")) {
this.ritualPos = BlockPos.fromLong(compound.getLong("ritual_pos"));
this.timer = compound.getInteger("timer");

View file

@ -154,6 +154,29 @@ public class PacketParticles implements IMessage {
0x5ccc30, 1F + world.rand.nextFloat() * 2F, 100, 0F, false, true);
break;
case 7: // Flower generator consumation
color = message.data[0];
for (int i = world.rand.nextInt(10) + 10; i >= 0; i--)
NaturesAura.proxy.spawnMagicParticle(world,
message.posX + 0.25F + world.rand.nextFloat() * 0.5F,
message.posY + 0.25F + world.rand.nextFloat() * 0.5F,
message.posZ + 0.25F + world.rand.nextFloat() * 0.5F,
world.rand.nextGaussian() * 0.01F,
world.rand.nextGaussian() * 0.01F,
world.rand.nextGaussian() * 0.01F,
color, world.rand.nextFloat() * 2F + 1F, 50, 0F, false, true);
break;
case 8: // Flower generator aura creation
for (int i = world.rand.nextInt(10) + 5; i >= 0; i--)
NaturesAura.proxy.spawnMagicParticle(world,
message.posX + 0.25F + world.rand.nextFloat() * 0.5F,
message.posY + 1.01F,
message.posZ + 0.25F + world.rand.nextFloat() * 0.5F,
world.rand.nextGaussian() * 0.005F,
world.rand.nextFloat() * 0.02F + 0.01F,
world.rand.nextGaussian() * 0.005F,
0x5ccc30, 1F + world.rand.nextFloat() * 1.5F, 80, 0F, false, true);
break;
}
}
});

View file

@ -0,0 +1,20 @@
{
"forge_marker": 1,
"defaults": {
"model": "minecraft:cube",
"textures": {
"particle": "naturesaura:blocks/flower_generator",
"up": "naturesaura:blocks/flower_generator_top",
"down": "naturesaura:blocks/flower_generator_top",
"north": "#particle",
"east": "#particle",
"south": "#particle",
"west": "#particle"
},
"transform": "forge:default-block"
},
"variants": {
"normal": [{}],
"inventory": [{}]
}
}

View file

@ -25,6 +25,7 @@ tile.naturesaura.infused_brick.name=Infused Brick
tile.naturesaura.infused_brick_stairs.name=Infused Brick Stairs
tile.naturesaura.infused_brick_slab.name=Infused Brick Slab
tile.naturesaura.infused_brick_slab_double.name=Infused Brick Double Slab
tile.naturesaura.flower_generator.name=Herbivorous Absorber
item.naturesaura.eye.name=Environmental Eye
item.naturesaura.gold_fiber.name=Brilliant Fiber

Binary file not shown.

After

Width:  |  Height:  |  Size: 465 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 426 B