mirror of
https://github.com/Ellpeck/NaturesAura.git
synced 2024-11-26 05:08:34 +01:00
item distributor, part 1
This commit is contained in:
parent
1b565e7800
commit
94a2092408
6 changed files with 159 additions and 7 deletions
|
@ -0,0 +1,34 @@
|
||||||
|
package de.ellpeck.naturesaura.blocks;
|
||||||
|
|
||||||
|
import de.ellpeck.naturesaura.blocks.tiles.TileEntityItemDistributor;
|
||||||
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.block.Blocks;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.tileentity.TileEntity;
|
||||||
|
import net.minecraft.util.ActionResultType;
|
||||||
|
import net.minecraft.util.Hand;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.util.math.BlockRayTraceResult;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
|
public class BlockItemDistributor extends BlockContainerImpl {
|
||||||
|
|
||||||
|
public BlockItemDistributor() {
|
||||||
|
super("item_distributor", TileEntityItemDistributor::new, ModBlocks.prop(Blocks.FURNACE));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
|
||||||
|
if (!player.isShiftKeyDown())
|
||||||
|
return ActionResultType.FAIL;
|
||||||
|
TileEntity tile = worldIn.getTileEntity(pos);
|
||||||
|
if (!(tile instanceof TileEntityItemDistributor))
|
||||||
|
return ActionResultType.FAIL;
|
||||||
|
if (!worldIn.isRemote) {
|
||||||
|
TileEntityItemDistributor distributor = (TileEntityItemDistributor) tile;
|
||||||
|
distributor.isRandomMode = !distributor.isRandomMode;
|
||||||
|
distributor.sendToClients();
|
||||||
|
}
|
||||||
|
return ActionResultType.SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
|
@ -61,6 +61,7 @@ public final class ModBlocks {
|
||||||
public static Block NETHER_WART_MUSHROOM;
|
public static Block NETHER_WART_MUSHROOM;
|
||||||
public static Block ANIMAL_CONTAINER;
|
public static Block ANIMAL_CONTAINER;
|
||||||
public static Block SNOW_CREATOR;
|
public static Block SNOW_CREATOR;
|
||||||
|
public static Block ITEM_DISTRIBUTOR;
|
||||||
|
|
||||||
public static Block.Properties prop(Material material, MaterialColor color) {
|
public static Block.Properties prop(Material material, MaterialColor color) {
|
||||||
return Block.Properties.create(material, color);
|
return Block.Properties.create(material, color);
|
||||||
|
|
|
@ -35,4 +35,5 @@ public final class ModTileEntities {
|
||||||
public static TileEntityType<TileEntityBlastFurnaceBooster> BLAST_FURNACE_BOOSTER;
|
public static TileEntityType<TileEntityBlastFurnaceBooster> BLAST_FURNACE_BOOSTER;
|
||||||
public static TileEntityType<TileEntityAnimalContainer> ANIMAL_CONTAINER;
|
public static TileEntityType<TileEntityAnimalContainer> ANIMAL_CONTAINER;
|
||||||
public static TileEntityType<TileEntitySnowCreator> SNOW_CREATOR;
|
public static TileEntityType<TileEntitySnowCreator> SNOW_CREATOR;
|
||||||
|
public static TileEntityType<TileEntityItemDistributor> ITEM_DISTRIBUTOR;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,107 @@
|
||||||
|
package de.ellpeck.naturesaura.blocks.tiles;
|
||||||
|
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.nbt.CompoundNBT;
|
||||||
|
import net.minecraft.tileentity.ITickableTileEntity;
|
||||||
|
import net.minecraft.tileentity.TileEntity;
|
||||||
|
import net.minecraft.util.Direction;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraftforge.items.CapabilityItemHandler;
|
||||||
|
import net.minecraftforge.items.IItemHandler;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TileEntityItemDistributor extends TileEntityImpl implements ITickableTileEntity {
|
||||||
|
|
||||||
|
private int cooldown;
|
||||||
|
private Direction currentSide = Direction.NORTH;
|
||||||
|
public boolean isRandomMode;
|
||||||
|
|
||||||
|
public TileEntityItemDistributor() {
|
||||||
|
super(ModTileEntities.ITEM_DISTRIBUTOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void tick() {
|
||||||
|
if (this.world.isRemote)
|
||||||
|
return;
|
||||||
|
if (this.cooldown > 0) {
|
||||||
|
this.cooldown--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.cooldown = 1;
|
||||||
|
|
||||||
|
IItemHandler above = this.getHandler(Direction.UP);
|
||||||
|
if (above == null)
|
||||||
|
return;
|
||||||
|
IItemHandler dest = this.getNextSide();
|
||||||
|
if (dest == null)
|
||||||
|
return;
|
||||||
|
for (int i = 0; i < above.getSlots(); i++) {
|
||||||
|
ItemStack stack = above.extractItem(i, 1, true);
|
||||||
|
if (stack.isEmpty())
|
||||||
|
continue;
|
||||||
|
for (int j = 0; j < dest.getSlots(); j++) {
|
||||||
|
ItemStack remain = dest.insertItem(j, stack, false);
|
||||||
|
if (!ItemStack.areItemStacksEqual(remain, stack)) {
|
||||||
|
above.extractItem(i, 1, false);
|
||||||
|
this.cooldown = 3;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private IItemHandler getHandler(Direction direction) {
|
||||||
|
BlockPos offset = this.pos.offset(direction);
|
||||||
|
TileEntity tile = this.world.getTileEntity(offset);
|
||||||
|
if (tile == null)
|
||||||
|
return null;
|
||||||
|
return tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite()).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IItemHandler getNextSide() {
|
||||||
|
if (this.isRandomMode) {
|
||||||
|
List<IItemHandler> handlers = new ArrayList<>();
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
IItemHandler handler = this.getHandler(Direction.byHorizontalIndex(i));
|
||||||
|
if (handler != null)
|
||||||
|
handlers.add(handler);
|
||||||
|
}
|
||||||
|
if (handlers.isEmpty())
|
||||||
|
return null;
|
||||||
|
return handlers.get(this.world.rand.nextInt(handlers.size()));
|
||||||
|
} else {
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
this.currentSide = this.currentSide.rotateY();
|
||||||
|
IItemHandler handler = this.getHandler(this.currentSide);
|
||||||
|
if (handler != null)
|
||||||
|
return handler;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeNBT(CompoundNBT compound, SaveType type) {
|
||||||
|
super.writeNBT(compound, type);
|
||||||
|
if (type == SaveType.TILE) {
|
||||||
|
compound.putInt("cooldown", this.cooldown);
|
||||||
|
compound.putInt("side", this.currentSide.ordinal());
|
||||||
|
}
|
||||||
|
if (type != SaveType.BLOCK)
|
||||||
|
compound.putBoolean("random", this.isRandomMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readNBT(CompoundNBT compound, SaveType type) {
|
||||||
|
super.readNBT(compound, type);
|
||||||
|
if (type == SaveType.TILE) {
|
||||||
|
this.cooldown = compound.getInt("cooldown");
|
||||||
|
this.currentSide = Direction.values()[compound.getInt("side")];
|
||||||
|
}
|
||||||
|
if (type != SaveType.BLOCK)
|
||||||
|
this.isRandomMode = compound.getBoolean("random");
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
|
||||||
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
|
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
|
||||||
import de.ellpeck.naturesaura.api.render.IVisualizable;
|
import de.ellpeck.naturesaura.api.render.IVisualizable;
|
||||||
import de.ellpeck.naturesaura.blocks.tiles.TileEntityGratedChute;
|
import de.ellpeck.naturesaura.blocks.tiles.TileEntityGratedChute;
|
||||||
|
import de.ellpeck.naturesaura.blocks.tiles.TileEntityItemDistributor;
|
||||||
import de.ellpeck.naturesaura.blocks.tiles.TileEntityNatureAltar;
|
import de.ellpeck.naturesaura.blocks.tiles.TileEntityNatureAltar;
|
||||||
import de.ellpeck.naturesaura.blocks.tiles.TileEntityRFConverter;
|
import de.ellpeck.naturesaura.blocks.tiles.TileEntityRFConverter;
|
||||||
import de.ellpeck.naturesaura.enchant.ModEnchantment;
|
import de.ellpeck.naturesaura.enchant.ModEnchantment;
|
||||||
|
@ -19,15 +20,13 @@ import de.ellpeck.naturesaura.items.ItemRangeVisualizer;
|
||||||
import de.ellpeck.naturesaura.items.ModItems;
|
import de.ellpeck.naturesaura.items.ModItems;
|
||||||
import de.ellpeck.naturesaura.packet.PacketAuraChunk;
|
import de.ellpeck.naturesaura.packet.PacketAuraChunk;
|
||||||
import de.ellpeck.naturesaura.particles.ParticleHandler;
|
import de.ellpeck.naturesaura.particles.ParticleHandler;
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.*;
|
||||||
import net.minecraft.block.BlockState;
|
|
||||||
import net.minecraft.block.IGrowable;
|
|
||||||
import net.minecraft.block.LeavesBlock;
|
|
||||||
import net.minecraft.client.MainWindow;
|
import net.minecraft.client.MainWindow;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.gui.AbstractGui;
|
import net.minecraft.client.gui.AbstractGui;
|
||||||
import net.minecraft.client.gui.screen.ChatScreen;
|
import net.minecraft.client.gui.screen.ChatScreen;
|
||||||
import net.minecraft.client.renderer.ActiveRenderInfo;
|
import net.minecraft.client.renderer.ActiveRenderInfo;
|
||||||
|
import net.minecraft.client.renderer.RenderHelper;
|
||||||
import net.minecraft.client.resources.I18n;
|
import net.minecraft.client.resources.I18n;
|
||||||
import net.minecraft.enchantment.Enchantment;
|
import net.minecraft.enchantment.Enchantment;
|
||||||
import net.minecraft.enchantment.EnchantmentHelper;
|
import net.minecraft.enchantment.EnchantmentHelper;
|
||||||
|
@ -71,6 +70,7 @@ public class ClientEvents {
|
||||||
public static final ResourceLocation BOOK_GUI = new ResourceLocation(NaturesAura.MOD_ID, "textures/gui/book.png");
|
public static final ResourceLocation BOOK_GUI = new ResourceLocation(NaturesAura.MOD_ID, "textures/gui/book.png");
|
||||||
public static final List<PacketAuraChunk> PENDING_AURA_CHUNKS = new ArrayList<>();
|
public static final List<PacketAuraChunk> PENDING_AURA_CHUNKS = new ArrayList<>();
|
||||||
private static final ItemStack ITEM_FRAME = new ItemStack(Items.ITEM_FRAME);
|
private static final ItemStack ITEM_FRAME = new ItemStack(Items.ITEM_FRAME);
|
||||||
|
private static final ItemStack DISPENSER = new ItemStack(Blocks.DISPENSER);
|
||||||
private static final Map<ResourceLocation, Tuple<ItemStack, Boolean>> SHOWING_EFFECTS = new HashMap<>();
|
private static final Map<ResourceLocation, Tuple<ItemStack, Boolean>> SHOWING_EFFECTS = new HashMap<>();
|
||||||
private static ItemStack heldCache = ItemStack.EMPTY;
|
private static ItemStack heldCache = ItemStack.EMPTY;
|
||||||
private static ItemStack heldEye = ItemStack.EMPTY;
|
private static ItemStack heldEye = ItemStack.EMPTY;
|
||||||
|
@ -396,6 +396,8 @@ public class ClientEvents {
|
||||||
if (pos != null) {
|
if (pos != null) {
|
||||||
TileEntity tile = mc.world.getTileEntity(pos);
|
TileEntity tile = mc.world.getTileEntity(pos);
|
||||||
IAuraContainer container;
|
IAuraContainer container;
|
||||||
|
int x = res.getScaledWidth() / 2;
|
||||||
|
int y = res.getScaledHeight() / 2;
|
||||||
if (tile != null && (container = tile.getCapability(NaturesAuraAPI.capAuraContainer, null).orElse(null)) != null) {
|
if (tile != null && (container = tile.getCapability(NaturesAuraAPI.capAuraContainer, null).orElse(null)) != null) {
|
||||||
BlockState state = mc.world.getBlockState(pos);
|
BlockState state = mc.world.getBlockState(pos);
|
||||||
ItemStack blockStack = state.getBlock().getPickBlock(state, mc.objectMouseOver, mc.world, pos, mc.player);
|
ItemStack blockStack = state.getBlock().getPickBlock(state, mc.objectMouseOver, mc.world, pos, mc.player);
|
||||||
|
@ -421,8 +423,6 @@ public class ClientEvents {
|
||||||
TileEntityGratedChute chute = (TileEntityGratedChute) tile;
|
TileEntityGratedChute chute = (TileEntityGratedChute) tile;
|
||||||
ItemStack stack = chute.getItemHandler(null).getStackInSlot(0);
|
ItemStack stack = chute.getItemHandler(null).getStackInSlot(0);
|
||||||
|
|
||||||
int x = res.getScaledWidth() / 2;
|
|
||||||
int y = res.getScaledHeight() / 2;
|
|
||||||
if (stack.isEmpty())
|
if (stack.isEmpty())
|
||||||
mc.fontRenderer.drawStringWithShadow(
|
mc.fontRenderer.drawStringWithShadow(
|
||||||
TextFormatting.GRAY.toString() + TextFormatting.ITALIC + I18n.format("info.naturesaura.empty"),
|
TextFormatting.GRAY.toString() + TextFormatting.ITALIC + I18n.format("info.naturesaura.empty"),
|
||||||
|
@ -436,6 +436,14 @@ public class ClientEvents {
|
||||||
GlStateManager.disableDepthTest();
|
GlStateManager.disableDepthTest();
|
||||||
AbstractGui.blit(x - 18, y - 18, u, 0, 16, 16, 256, 256);
|
AbstractGui.blit(x - 18, y - 18, u, 0, 16, 16, 256, 256);
|
||||||
GlStateManager.enableDepthTest();
|
GlStateManager.enableDepthTest();
|
||||||
|
} else if (tile instanceof TileEntityItemDistributor) {
|
||||||
|
TileEntityItemDistributor distributor = (TileEntityItemDistributor) tile;
|
||||||
|
Helper.renderItemInGui(DISPENSER, x - 24, y - 24, 1F);
|
||||||
|
mc.getTextureManager().bindTexture(OVERLAYS);
|
||||||
|
int u = !distributor.isRandomMode ? 240 : 224;
|
||||||
|
GlStateManager.disableDepthTest();
|
||||||
|
AbstractGui.blit(x - 18, y - 18, u, 0, 16, 16, 256, 256);
|
||||||
|
GlStateManager.enableDepthTest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,7 +109,8 @@ public final class ModRegistry {
|
||||||
new BlockBlastFurnaceBooster(),
|
new BlockBlastFurnaceBooster(),
|
||||||
new BlockImpl("nether_wart_mushroom", ModBlocks.prop(Blocks.RED_MUSHROOM_BLOCK)),
|
new BlockImpl("nether_wart_mushroom", ModBlocks.prop(Blocks.RED_MUSHROOM_BLOCK)),
|
||||||
new BlockAnimalContainer(),
|
new BlockAnimalContainer(),
|
||||||
new BlockSnowCreator()
|
new BlockSnowCreator(),
|
||||||
|
new BlockItemDistributor()
|
||||||
);
|
);
|
||||||
|
|
||||||
if (ModConfig.instance.rfConverter.get())
|
if (ModConfig.instance.rfConverter.get())
|
||||||
|
|
Loading…
Reference in a new issue