Compare commits

...

5 commits

Author SHA1 Message Date
Ell
ae5128971c resolved all todos (untested though oof) 2021-12-02 22:00:00 +01:00
Ell
1d9c22cb0a var 2021-12-02 17:46:56 +01:00
Ell
0df6401c4e IT COMPILES!! 2021-12-02 16:55:04 +01:00
Ell
46320dd889 off to get some food now 2021-12-02 15:25:46 +01:00
Ell
077b09c37d PipeNetwork is error-free! 2021-12-02 14:44:26 +01:00
76 changed files with 1977 additions and 2095 deletions

View file

@ -95,7 +95,7 @@ repositories {
configurations { configurations {
embed embed
compile.extendsFrom(embed) implementation.extendsFrom(embed)
} }
dependencies { dependencies {

View file

@ -8,8 +8,8 @@ import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.packets.PacketHandler; import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.pipe.IPipeConnectable; import de.ellpeck.prettypipes.pipe.IPipeConnectable;
import de.ellpeck.prettypipes.pipe.PipeBlock; import de.ellpeck.prettypipes.pipe.PipeBlock;
import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.PipeRenderer; import de.ellpeck.prettypipes.pipe.PipeRenderer;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import de.ellpeck.prettypipes.pipe.containers.MainPipeContainer; import de.ellpeck.prettypipes.pipe.containers.MainPipeContainer;
import de.ellpeck.prettypipes.pipe.containers.MainPipeGui; import de.ellpeck.prettypipes.pipe.containers.MainPipeGui;
@ -36,39 +36,37 @@ import de.ellpeck.prettypipes.pipe.modules.stacksize.StackSizeModuleContainer;
import de.ellpeck.prettypipes.pipe.modules.stacksize.StackSizeModuleGui; import de.ellpeck.prettypipes.pipe.modules.stacksize.StackSizeModuleGui;
import de.ellpeck.prettypipes.pipe.modules.stacksize.StackSizeModuleItem; import de.ellpeck.prettypipes.pipe.modules.stacksize.StackSizeModuleItem;
import de.ellpeck.prettypipes.pressurizer.PressurizerBlock; import de.ellpeck.prettypipes.pressurizer.PressurizerBlock;
import de.ellpeck.prettypipes.pressurizer.PressurizerBlockEntity;
import de.ellpeck.prettypipes.pressurizer.PressurizerContainer; import de.ellpeck.prettypipes.pressurizer.PressurizerContainer;
import de.ellpeck.prettypipes.pressurizer.PressurizerGui; import de.ellpeck.prettypipes.pressurizer.PressurizerGui;
import de.ellpeck.prettypipes.pressurizer.PressurizerBlockEntity;
import de.ellpeck.prettypipes.terminal.CraftingTerminalBlock; import de.ellpeck.prettypipes.terminal.CraftingTerminalBlock;
import de.ellpeck.prettypipes.terminal.CraftingTerminalTileEntity; import de.ellpeck.prettypipes.terminal.CraftingTerminalBlockEntity;
import de.ellpeck.prettypipes.terminal.ItemTerminalBlock; import de.ellpeck.prettypipes.terminal.ItemTerminalBlock;
import de.ellpeck.prettypipes.terminal.ItemTerminalTileEntity; import de.ellpeck.prettypipes.terminal.ItemTerminalBlockEntity;
import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer; import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer;
import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalGui; import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalGui;
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalContainer; import de.ellpeck.prettypipes.terminal.containers.ItemTerminalContainer;
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui; import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui;
import net.minecraft.block.Block; import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.gui.ScreenManager; import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup; import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
import net.minecraft.entity.EntityClassification; import net.minecraft.client.renderer.entity.EntityRenderers;
import net.minecraft.entity.EntityType; import net.minecraft.world.entity.EntityType;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.world.entity.MobCategory;
import net.minecraft.item.BlockItem; import net.minecraft.world.inventory.MenuType;
import net.minecraft.item.Item; import net.minecraft.world.item.BlockItem;
import net.minecraft.item.ItemGroup; import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.INBT; import net.minecraft.world.level.block.Block;
import net.minecraft.tileentity.TileEntityType; import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager; import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.extensions.IForgeContainerType; import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.common.extensions.IForgeMenuType;
import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
@ -76,7 +74,6 @@ import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IForgeRegistry; import net.minecraftforge.registries.IForgeRegistry;
import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -86,46 +83,46 @@ import java.util.function.BiFunction;
@Mod.EventBusSubscriber(bus = Bus.MOD) @Mod.EventBusSubscriber(bus = Bus.MOD)
public final class Registry { public final class Registry {
public static final ItemGroup GROUP = new ItemGroup(PrettyPipes.ID) { public static final CreativeModeTab TAB = new CreativeModeTab(PrettyPipes.ID) {
@Override @Override
public ItemStack createIcon() { public ItemStack makeIcon() {
return new ItemStack(wrenchItem); return new ItemStack(wrenchItem);
} }
}; };
@CapabilityInject(PipeNetwork.class) public static Capability<PipeNetwork> pipeNetworkCapability = CapabilityManager.get(new CapabilityToken<>() {
public static Capability<PipeNetwork> pipeNetworkCapability; });
@CapabilityInject(IPipeConnectable.class) public static Capability<IPipeConnectable> pipeConnectableCapability = CapabilityManager.get(new CapabilityToken<>() {
public static Capability<IPipeConnectable> pipeConnectableCapability; });
public static Item wrenchItem; public static Item wrenchItem;
public static Item pipeFrameItem; public static Item pipeFrameItem;
public static Block pipeBlock; public static Block pipeBlock;
public static TileEntityType<PipeTileEntity> pipeTileEntity; public static BlockEntityType<PipeBlockEntity> pipeBlockEntity;
public static ContainerType<MainPipeContainer> pipeContainer; public static MenuType<MainPipeContainer> pipeContainer;
public static Block itemTerminalBlock; public static Block itemTerminalBlock;
public static TileEntityType<ItemTerminalTileEntity> itemTerminalTileEntity; public static BlockEntityType<ItemTerminalBlockEntity> itemTerminalBlockEntity;
public static ContainerType<ItemTerminalContainer> itemTerminalContainer; public static MenuType<ItemTerminalContainer> itemTerminalContainer;
public static Block craftingTerminalBlock; public static Block craftingTerminalBlock;
public static TileEntityType<CraftingTerminalTileEntity> craftingTerminalTileEntity; public static BlockEntityType<CraftingTerminalBlockEntity> craftingTerminalBlockEntity;
public static ContainerType<CraftingTerminalContainer> craftingTerminalContainer; public static MenuType<CraftingTerminalContainer> craftingTerminalContainer;
public static EntityType<PipeFrameEntity> pipeFrameEntity; public static EntityType<PipeFrameEntity> pipeFrameEntity;
public static Block pressurizerBlock; public static Block pressurizerBlock;
public static TileEntityType<PressurizerBlockEntity> pressurizerTileEntity; public static BlockEntityType<PressurizerBlockEntity> pressurizerBlockEntity;
public static ContainerType<PressurizerContainer> pressurizerContainer; public static MenuType<PressurizerContainer> pressurizerContainer;
public static ContainerType<ExtractionModuleContainer> extractionModuleContainer; public static MenuType<ExtractionModuleContainer> extractionModuleContainer;
public static ContainerType<FilterModuleContainer> filterModuleContainer; public static MenuType<FilterModuleContainer> filterModuleContainer;
public static ContainerType<RetrievalModuleContainer> retrievalModuleContainer; public static MenuType<RetrievalModuleContainer> retrievalModuleContainer;
public static ContainerType<StackSizeModuleContainer> stackSizeModuleContainer; public static MenuType<StackSizeModuleContainer> stackSizeModuleContainer;
public static ContainerType<FilterIncreaseModuleContainer> filterIncreaseModuleContainer; public static MenuType<FilterIncreaseModuleContainer> filterIncreaseModuleContainer;
public static ContainerType<CraftingModuleContainer> craftingModuleContainer; public static MenuType<CraftingModuleContainer> craftingModuleContainer;
public static ContainerType<FilterModifierModuleContainer> filterModifierModuleContainer; public static MenuType<FilterModifierModuleContainer> filterModifierModuleContainer;
@SubscribeEvent @SubscribeEvent
public static void registerBlocks(RegistryEvent.Register<Block> event) { public static void registerBlocks(RegistryEvent.Register<Block> event) {
@ -139,10 +136,10 @@ public final class Registry {
@SubscribeEvent @SubscribeEvent
public static void registerItems(RegistryEvent.Register<Item> event) { public static void registerItems(RegistryEvent.Register<Item> event) {
IForgeRegistry<Item> registry = event.getRegistry(); var registry = event.getRegistry();
registry.registerAll( registry.registerAll(
wrenchItem = new WrenchItem().setRegistryName("wrench"), wrenchItem = new WrenchItem().setRegistryName("wrench"),
new Item(new Item.Properties().group(GROUP)).setRegistryName("blank_module"), new Item(new Item.Properties().tab(TAB)).setRegistryName("blank_module"),
pipeFrameItem = new PipeFrameItem().setRegistryName("pipe_frame") pipeFrameItem = new PipeFrameItem().setRegistryName("pipe_frame")
); );
registry.registerAll(createTieredModule("extraction_module", ExtractionModuleItem::new)); registry.registerAll(createTieredModule("extraction_module", ExtractionModuleItem::new));
@ -160,33 +157,33 @@ public final class Registry {
ForgeRegistries.BLOCKS.getValues().stream() ForgeRegistries.BLOCKS.getValues().stream()
.filter(b -> b.getRegistryName().getNamespace().equals(PrettyPipes.ID)) .filter(b -> b.getRegistryName().getNamespace().equals(PrettyPipes.ID))
.forEach(b -> registry.register(new BlockItem(b, new Item.Properties().group(GROUP)).setRegistryName(b.getRegistryName()))); .forEach(b -> registry.register(new BlockItem(b, new Item.Properties().tab(TAB)).setRegistryName(b.getRegistryName())));
} }
@SubscribeEvent @SubscribeEvent
public static void registerTiles(RegistryEvent.Register<TileEntityType<?>> event) { public static void registerBlockEntities(RegistryEvent.Register<BlockEntityType<?>> event) {
event.getRegistry().registerAll( event.getRegistry().registerAll(
pipeTileEntity = (TileEntityType<PipeTileEntity>) TileEntityType.Builder.create(PipeTileEntity::new, pipeBlock).build(null).setRegistryName("pipe"), pipeBlockEntity = (BlockEntityType<PipeBlockEntity>) BlockEntityType.Builder.of(PipeBlockEntity::new, pipeBlock).build(null).setRegistryName("pipe"),
itemTerminalTileEntity = (TileEntityType<ItemTerminalTileEntity>) TileEntityType.Builder.create(ItemTerminalTileEntity::new, itemTerminalBlock).build(null).setRegistryName("item_terminal"), itemTerminalBlockEntity = (BlockEntityType<ItemTerminalBlockEntity>) BlockEntityType.Builder.of(ItemTerminalBlockEntity::new, itemTerminalBlock).build(null).setRegistryName("item_terminal"),
craftingTerminalTileEntity = (TileEntityType<CraftingTerminalTileEntity>) TileEntityType.Builder.create(CraftingTerminalTileEntity::new, craftingTerminalBlock).build(null).setRegistryName("crafting_terminal"), craftingTerminalBlockEntity = (BlockEntityType<CraftingTerminalBlockEntity>) BlockEntityType.Builder.of(CraftingTerminalBlockEntity::new, craftingTerminalBlock).build(null).setRegistryName("crafting_terminal"),
pressurizerTileEntity = (TileEntityType<PressurizerBlockEntity>) TileEntityType.Builder.create(PressurizerBlockEntity::new, pressurizerBlock).build(null).setRegistryName("pressurizer") pressurizerBlockEntity = (BlockEntityType<PressurizerBlockEntity>) BlockEntityType.Builder.of(PressurizerBlockEntity::new, pressurizerBlock).build(null).setRegistryName("pressurizer")
); );
} }
@SubscribeEvent @SubscribeEvent
public static void registerEntities(RegistryEvent.Register<EntityType<?>> event) { public static void registerEntities(RegistryEvent.Register<EntityType<?>> event) {
event.getRegistry().registerAll( event.getRegistry().registerAll(
pipeFrameEntity = (EntityType<PipeFrameEntity>) EntityType.Builder.<PipeFrameEntity>create(PipeFrameEntity::new, EntityClassification.MISC).build("pipe_frame").setRegistryName("pipe_frame") pipeFrameEntity = (EntityType<PipeFrameEntity>) EntityType.Builder.<PipeFrameEntity>of(PipeFrameEntity::new, MobCategory.MISC).build("pipe_frame").setRegistryName("pipe_frame")
); );
} }
@SubscribeEvent @SubscribeEvent
public static void registerContainers(RegistryEvent.Register<ContainerType<?>> event) { public static void registerContainers(RegistryEvent.Register<MenuType<?>> event) {
event.getRegistry().registerAll( event.getRegistry().registerAll(
pipeContainer = (ContainerType<MainPipeContainer>) IForgeContainerType.create((windowId, inv, data) -> new MainPipeContainer(pipeContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("pipe"), pipeContainer = (MenuType<MainPipeContainer>) IForgeMenuType.create((windowId, inv, data) -> new MainPipeContainer(pipeContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("pipe"),
itemTerminalContainer = (ContainerType<ItemTerminalContainer>) IForgeContainerType.create((windowId, inv, data) -> new ItemTerminalContainer(itemTerminalContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("item_terminal"), itemTerminalContainer = (MenuType<ItemTerminalContainer>) IForgeMenuType.create((windowId, inv, data) -> new ItemTerminalContainer(itemTerminalContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("item_terminal"),
craftingTerminalContainer = (ContainerType<CraftingTerminalContainer>) IForgeContainerType.create((windowId, inv, data) -> new CraftingTerminalContainer(craftingTerminalContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("crafting_terminal"), craftingTerminalContainer = (MenuType<CraftingTerminalContainer>) IForgeMenuType.create((windowId, inv, data) -> new CraftingTerminalContainer(craftingTerminalContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("crafting_terminal"),
pressurizerContainer = (ContainerType<PressurizerContainer>) IForgeContainerType.create((windowId, inv, data) -> new PressurizerContainer(pressurizerContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("pressurizer"), pressurizerContainer = (MenuType<PressurizerContainer>) IForgeMenuType.create((windowId, inv, data) -> new PressurizerContainer(pressurizerContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("pressurizer"),
extractionModuleContainer = createPipeContainer("extraction_module"), extractionModuleContainer = createPipeContainer("extraction_module"),
filterModuleContainer = createPipeContainer("filter_module"), filterModuleContainer = createPipeContainer("filter_module"),
retrievalModuleContainer = createPipeContainer("retrieval_module"), retrievalModuleContainer = createPipeContainer("retrieval_module"),
@ -197,60 +194,44 @@ public final class Registry {
); );
} }
private static <T extends AbstractPipeContainer<?>> ContainerType<T> createPipeContainer(String name) { private static <T extends AbstractPipeContainer<?>> MenuType<T> createPipeContainer(String name) {
return (ContainerType<T>) IForgeContainerType.create((windowId, inv, data) -> { return (MenuType<T>) IForgeMenuType.create((windowId, inv, data) -> {
PipeTileEntity tile = Utility.getBlockEntity(PipeTileEntity.class, inv.player.world, data.readBlockPos()); var tile = Utility.getBlockEntity(PipeBlockEntity.class, inv.player.level, data.readBlockPos());
int moduleIndex = data.readInt(); var moduleIndex = data.readInt();
ItemStack moduleStack = tile.modules.getStackInSlot(moduleIndex); var moduleStack = tile.modules.getStackInSlot(moduleIndex);
return ((IModule) moduleStack.getItem()).getContainer(moduleStack, tile, windowId, inv, inv.player, moduleIndex); return ((IModule) moduleStack.getItem()).getContainer(moduleStack, tile, windowId, inv, inv.player, moduleIndex);
}).setRegistryName(name); }).setRegistryName(name);
} }
private static Item[] createTieredModule(String name, BiFunction<String, ModuleTier, ModuleItem> item) { private static Item[] createTieredModule(String name, BiFunction<String, ModuleTier, ModuleItem> item) {
List<Item> items = new ArrayList<>(); List<Item> items = new ArrayList<>();
for (ModuleTier tier : ModuleTier.values()) for (var tier : ModuleTier.values())
items.add(item.apply(name, tier).setRegistryName(tier.name().toLowerCase(Locale.ROOT) + "_" + name)); items.add(item.apply(name, tier).setRegistryName(tier.name().toLowerCase(Locale.ROOT) + "_" + name));
return items.toArray(new Item[0]); return items.toArray(new Item[0]);
} }
public static void setup(FMLCommonSetupEvent event) { public static void setup(FMLCommonSetupEvent event) {
registerCap(PipeNetwork.class);
registerCap(IPipeConnectable.class);
PacketHandler.setup(); PacketHandler.setup();
} }
public static final class Client { public static final class Client {
public static void setup(FMLClientSetupEvent event) {
RenderTypeLookup.setRenderLayer(pipeBlock, RenderType.getCutout());
ClientRegistry.bindTileEntityRenderer(pipeTileEntity, PipeRenderer::new);
RenderingRegistry.registerEntityRenderingHandler(pipeFrameEntity, PipeFrameRenderer::new);
ScreenManager.registerFactory(pipeContainer, MainPipeGui::new); public static void setup(FMLClientSetupEvent event) {
ScreenManager.registerFactory(itemTerminalContainer, ItemTerminalGui::new); ItemBlockRenderTypes.setRenderLayer(pipeBlock, RenderType.cutout());
ScreenManager.registerFactory(pressurizerContainer, PressurizerGui::new); BlockEntityRenderers.register(pipeBlockEntity, PipeRenderer::new);
ScreenManager.registerFactory(craftingTerminalContainer, CraftingTerminalGui::new); EntityRenderers.register(pipeFrameEntity, PipeFrameRenderer::new);
ScreenManager.registerFactory(extractionModuleContainer, ExtractionModuleGui::new);
ScreenManager.registerFactory(filterModuleContainer, FilterModuleGui::new); MenuScreens.register(pipeContainer, MainPipeGui::new);
ScreenManager.registerFactory(retrievalModuleContainer, RetrievalModuleGui::new); MenuScreens.register(itemTerminalContainer, ItemTerminalGui::new);
ScreenManager.registerFactory(stackSizeModuleContainer, StackSizeModuleGui::new); MenuScreens.register(pressurizerContainer, PressurizerGui::new);
ScreenManager.registerFactory(filterIncreaseModuleContainer, FilterIncreaseModuleGui::new); MenuScreens.register(craftingTerminalContainer, CraftingTerminalGui::new);
ScreenManager.registerFactory(craftingModuleContainer, CraftingModuleGui::new); MenuScreens.register(extractionModuleContainer, ExtractionModuleGui::new);
ScreenManager.registerFactory(filterModifierModuleContainer, FilterModifierModuleGui::new); MenuScreens.register(filterModuleContainer, FilterModuleGui::new);
MenuScreens.register(retrievalModuleContainer, RetrievalModuleGui::new);
MenuScreens.register(stackSizeModuleContainer, StackSizeModuleGui::new);
MenuScreens.register(filterIncreaseModuleContainer, FilterIncreaseModuleGui::new);
MenuScreens.register(craftingModuleContainer, CraftingModuleGui::new);
MenuScreens.register(filterModifierModuleContainer, FilterModifierModuleGui::new);
} }
} }
private static <T> void registerCap(Class<T> capClass) {
CapabilityManager.INSTANCE.register(capClass, new Capability.IStorage<T>() {
@Nullable
@Override
public INBT writeNBT(Capability<T> capability, T instance, Direction side) {
return null;
}
@Override
public void readNBT(Capability<T> capability, T instance, Direction side, INBT nbt) {
}
}, () -> null);
}
} }

View file

@ -5,25 +5,20 @@ import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.resources.language.I18n; import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.chat.*; import net.minecraft.network.chat.*;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Containers; import net.minecraft.world.Containers;
import net.minecraft.world.WorldlyContainer;
import net.minecraft.world.WorldlyContainerHolder; import net.minecraft.world.WorldlyContainerHolder;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.world.level.BlockGetter;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.wrapper.SidedInvWrapper; import net.minecraftforge.items.wrapper.SidedInvWrapper;
@ -36,7 +31,7 @@ import java.util.function.Function;
public final class Utility { public final class Utility {
public static <T extends BlockEntity> T getBlockEntity(Class<T> type, LevelAccessor world, BlockPos pos) { public static <T extends BlockEntity> T getBlockEntity(Class<T> type, BlockGetter world, BlockPos pos) {
var tile = world.getBlockEntity(pos); var tile = world.getBlockEntity(pos);
return type.isInstance(tile) ? (T) tile : null; return type.isInstance(tile) ? (T) tile : null;
} }
@ -55,7 +50,7 @@ public final class Utility {
return Direction.fromNormal(diff.getX(), diff.getY(), diff.getZ()); return Direction.fromNormal(diff.getX(), diff.getY(), diff.getZ());
} }
public static void addTooltip(String name, List<MutableComponent> tooltip) { public static void addTooltip(String name, List<Component> tooltip) {
if (Screen.hasShiftDown()) { if (Screen.hasShiftDown()) {
var content = I18n.get("info." + PrettyPipes.ID + "." + name).split("\n"); var content = I18n.get("info." + PrettyPipes.ID + "." + name).split("\n");
for (var s : content) for (var s : content)

View file

@ -1,42 +1,29 @@
package de.ellpeck.prettypipes.entities; package de.ellpeck.prettypipes.entities;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.network.NetworkLocation;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.PipeBlock; import de.ellpeck.prettypipes.pipe.PipeBlock;
import net.minecraft.block.BlockState;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.entity.Entity; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.item.ItemFrameEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.FilledMapItem;
import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.decoration.ItemFrame; import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.item.Items; import net.minecraft.world.level.GameRules;
import net.minecraft.network.IPacket;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.GameRules;
import net.minecraft.world.World;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.HitResult;
import net.minecraft.world.storage.MapData;
import net.minecraftforge.entity.IEntityAdditionalSpawnData; import net.minecraftforge.entity.IEntityAdditionalSpawnData;
import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
import net.minecraftforge.fml.network.NetworkHooks;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.List;
public class PipeFrameEntity extends ItemFrame implements IEntityAdditionalSpawnData { public class PipeFrameEntity extends ItemFrame implements IEntityAdditionalSpawnData {
@ -65,15 +52,15 @@ public class PipeFrameEntity extends ItemFrame implements IEntityAdditionalSpawn
return; return;
if (this.tickCount % 40 != 0) if (this.tickCount % 40 != 0)
return; return;
PipeNetwork network = PipeNetwork.get(this.level); var network = PipeNetwork.get(this.level);
BlockPos attached = getAttachedPipe(this.level, this.pos, this.direction); var attached = getAttachedPipe(this.level, this.pos, this.direction);
if (attached != null) { if (attached != null) {
BlockPos node = network.getNodeFromPipe(attached); var node = network.getNodeFromPipe(attached);
if (node != null) { if (node != null) {
ItemStack stack = this.getItem(); var stack = this.getItem();
if (!stack.isEmpty()) { if (!stack.isEmpty()) {
List<NetworkLocation> items = network.getOrderedNetworkItems(node); var items = network.getOrderedNetworkItems(node);
int amount = items.stream().mapToInt(i -> i.getItemAmount(this.level, stack)).sum(); var amount = items.stream().mapToInt(i -> i.getItemAmount(this.level, stack)).sum();
this.entityData.set(AMOUNT, amount); this.entityData.set(AMOUNT, amount);
return; return;
} }
@ -83,103 +70,97 @@ public class PipeFrameEntity extends ItemFrame implements IEntityAdditionalSpawn
} }
@Override @Override
public boolean onValidSurface() { public boolean survives() {
return super.onValidSurface() && canPlace(this.world, this.hangingPosition, this.facingDirection); return super.survives() && canPlace(this.level, this.pos, this.direction);
} }
private static BlockPos getAttachedPipe(Level world, BlockPos pos, Direction direction) { private static BlockPos getAttachedPipe(Level world, BlockPos pos, Direction direction) {
for (int i = 1; i <= 2; i++) { for (var i = 1; i <= 2; i++) {
BlockPos offset = pos.relative(direction.getOpposite(), i); var offset = pos.relative(direction.getOpposite(), i);
BlockState state = world.getBlockState(offset); var state = world.getBlockState(offset);
if (state.getBlock() instanceof PipeBlock) if (state.getBlock() instanceof PipeBlock)
return offset; return offset;
} }
return null; return null;
} }
public static boolean canPlace(World world, BlockPos pos, Direction direction) { public static boolean canPlace(Level world, BlockPos pos, Direction direction) {
return getAttachedPipe(world, pos, direction) != null; return getAttachedPipe(world, pos, direction) != null;
} }
public int getAmount() { public int getAmount() {
return this.dataManager.get(AMOUNT); return this.entityData.get(AMOUNT);
} }
@Override @Override
public boolean attackEntityFrom(DamageSource source, float amount) { public boolean hurt(DamageSource source, float amount) {
if (this.isInvulnerableTo(source)) { if (this.isInvulnerableTo(source)) {
return false; return false;
} else if (!source.isExplosion() && !this.getDisplayedItem().isEmpty()) { } else if (!source.isExplosion() && !this.getItem().isEmpty()) {
if (!this.world.isRemote) { if (!this.level.isClientSide) {
this.dropItemOrSelf(source.getTrueSource(), false); this.dropItemOrSelf(source.getDirectEntity(), false);
this.playSound(SoundEvents.ENTITY_ITEM_FRAME_REMOVE_ITEM, 1.0F, 1.0F); this.playSound(SoundEvents.ITEM_FRAME_REMOVE_ITEM, 1.0F, 1.0F);
} }
return true; return true;
} else { } else {
return super.attackEntityFrom(source, amount); return super.hurt(source, amount);
} }
} }
@Override @Override
public void onBroken(@Nullable Entity brokenEntity) { public void dropItem(@Nullable Entity brokenEntity) {
this.playSound(SoundEvents.ENTITY_ITEM_FRAME_BREAK, 1.0F, 1.0F); this.playSound(SoundEvents.ITEM_FRAME_BREAK, 1.0F, 1.0F);
this.dropItemOrSelf(brokenEntity, true); this.dropItemOrSelf(brokenEntity, true);
} }
private void dropItemOrSelf(@Nullable Entity entityIn, boolean b) { private void dropItemOrSelf(@Nullable Entity entityIn, boolean b) {
if (!this.world.getGameRules().getBoolean(GameRules.DO_ENTITY_DROPS)) { if (!this.level.getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
if (entityIn == null) if (entityIn == null)
this.getDisplayedItem().setAttachedEntity(null); this.getItem().setEntityRepresentation(null);
} else { } else {
ItemStack itemstack = this.getDisplayedItem(); var itemstack = this.getItem();
this.setDisplayedItem(ItemStack.EMPTY); this.setItem(ItemStack.EMPTY);
if (entityIn instanceof PlayerEntity) { if (entityIn instanceof Player playerentity) {
PlayerEntity playerentity = (PlayerEntity) entityIn; if (playerentity.isCreative()) {
if (playerentity.abilities.isCreativeMode) { itemstack.setEntityRepresentation(null);
itemstack.setAttachedEntity(null);
return; return;
} }
} }
if (b) if (b)
this.entityDropItem(Registry.pipeFrameItem); this.spawnAtLocation(Registry.pipeFrameItem);
if (!itemstack.isEmpty()) { if (!itemstack.isEmpty()) {
itemstack = itemstack.copy(); itemstack = itemstack.copy();
itemstack.setAttachedEntity(null); itemstack.setEntityRepresentation(null);
this.entityDropItem(itemstack); this.spawnAtLocation(itemstack);
} }
} }
} }
@Override @Override
public ActionResultType processInitialInteract(PlayerEntity player, Hand hand) { public InteractionResult interact(Player player, InteractionHand hand) {
if (this.getDisplayedItem().isEmpty()) if (this.getItem().isEmpty())
return super.processInitialInteract(player, hand); return super.interact(player, hand);
return ActionResultType.FAIL; return InteractionResult.FAIL;
} }
@Override @Override
public ItemStack getPickedResult(RayTraceResult target) { public ItemStack getPickedResult(HitResult target) {
return new ItemStack(Registry.pipeFrameItem); return new ItemStack(Registry.pipeFrameItem);
} }
@Override @Override
public IPacket<?> createSpawnPacket() { public void writeSpawnData(FriendlyByteBuf buffer) {
return NetworkHooks.getEntitySpawningPacket(this); buffer.writeBlockPos(this.pos);
buffer.writeInt(this.direction.ordinal());
} }
@Override @Override
public void writeSpawnData(PacketBuffer buffer) { public void readSpawnData(FriendlyByteBuf additionalData) {
buffer.writeBlockPos(this.hangingPosition); this.pos = additionalData.readBlockPos();
buffer.writeInt(this.facingDirection.getIndex()); this.direction = Direction.values()[additionalData.readInt()];
}
@Override
public void readSpawnData(PacketBuffer additionalData) {
this.hangingPosition = additionalData.readBlockPos();
this.updateFacingWithBoundingBox(Direction.values()[additionalData.readInt()]);
} }
} }

View file

@ -1,42 +1,37 @@
package de.ellpeck.prettypipes.entities; package de.ellpeck.prettypipes.entities;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft; import com.mojang.math.Vector3f;
import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.entity.EntityRendererManager;
import net.minecraft.client.renderer.entity.ItemFrameRenderer; import net.minecraft.client.renderer.entity.ItemFrameRenderer;
import net.minecraft.entity.item.ItemFrameEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.math.vector.Matrix4f;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.math.vector.Vector3f;
public class PipeFrameRenderer extends ItemFrameRenderer { public class PipeFrameRenderer extends ItemFrameRenderer<PipeFrameEntity> {
public PipeFrameRenderer(EntityRendererManager renderManagerIn) {
super(renderManagerIn, Minecraft.getInstance().getItemRenderer()); public PipeFrameRenderer(EntityRendererProvider.Context renderManagerIn) {
super(renderManagerIn);
} }
@Override @Override
public void render(ItemFrameEntity entityIn, float entityYaw, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int packedLightIn) { public void render(PipeFrameEntity entityIn, float entityYaw, float partialTicks, PoseStack matrixStackIn, MultiBufferSource bufferIn, int packedLightIn) {
super.render(entityIn, entityYaw, partialTicks, matrixStackIn, bufferIn, packedLightIn); super.render(entityIn, entityYaw, partialTicks, matrixStackIn, bufferIn, packedLightIn);
matrixStackIn.push(); matrixStackIn.pushPose();
Direction direction = entityIn.getHorizontalFacing(); var direction = entityIn.getDirection();
Vector3d vec3d = this.getRenderOffset(entityIn, partialTicks); var vec3d = this.getRenderOffset(entityIn, partialTicks);
matrixStackIn.translate(-vec3d.getX(), -vec3d.getY(), -vec3d.getZ()); matrixStackIn.translate(-vec3d.x, -vec3d.y, -vec3d.z);
matrixStackIn.translate(direction.getXOffset() * 0.46875, direction.getYOffset() * 0.46875, direction.getZOffset() * 0.46875); matrixStackIn.translate(direction.getStepX() * 0.46875, direction.getStepY() * 0.46875, direction.getStepZ() * 0.46875);
matrixStackIn.rotate(Vector3f.XP.rotationDegrees(entityIn.rotationPitch)); matrixStackIn.mulPose(Vector3f.XP.rotationDegrees(entityIn.getXRot()));
matrixStackIn.rotate(Vector3f.YP.rotationDegrees(180.0F - entityIn.rotationYaw)); matrixStackIn.mulPose(Vector3f.YP.rotationDegrees(180.0F - entityIn.getYRot()));
FontRenderer font = this.getFontRendererFromRenderManager(); var font = this.getFont();
int amount = ((PipeFrameEntity) entityIn).getAmount(); var amount = entityIn.getAmount();
String ammountStrg = amount < 0 ? "?" : String.valueOf(amount); var ammountStrg = amount < 0 ? "?" : String.valueOf(amount);
float x = 0.5F - font.getStringWidth(ammountStrg) / 2F; var x = 0.5F - font.width(ammountStrg) / 2F;
Matrix4f matrix4f = matrixStackIn.getLast().getMatrix(); var matrix4f = matrixStackIn.last().pose();
matrixStackIn.translate(0, 0.285F, 0.415F); matrixStackIn.translate(0, 0.285F, 0.415F);
matrixStackIn.scale(-0.02F, -0.02F, 0.02F); matrixStackIn.scale(-0.02F, -0.02F, 0.02F);
font.renderString(ammountStrg, x, 0, 0xFFFFFF, true, matrix4f, bufferIn, false, 0, packedLightIn); font.drawInBatch(ammountStrg, x, 0, 0xFFFFFF, true, matrix4f, bufferIn, false, 0, packedLightIn);
matrixStackIn.pop(); matrixStackIn.popPose();
} }
} }

View file

@ -1,12 +1,12 @@
package de.ellpeck.prettypipes.items; package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import java.util.List; import java.util.List;
@ -15,33 +15,33 @@ import java.util.function.Consumer;
public interface IModule { public interface IModule {
void tick(ItemStack module, PipeTileEntity tile); void tick(ItemStack module, PipeBlockEntity tile);
boolean canNetworkSee(ItemStack module, PipeTileEntity tile); boolean canNetworkSee(ItemStack module, PipeBlockEntity tile);
boolean canAcceptItem(ItemStack module, PipeTileEntity tile, ItemStack stack); boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack);
int getMaxInsertionAmount(ItemStack module, PipeTileEntity tile, ItemStack stack, IItemHandler destination); int getMaxInsertionAmount(ItemStack module, PipeBlockEntity tile, ItemStack stack, IItemHandler destination);
int getPriority(ItemStack module, PipeTileEntity tile); int getPriority(ItemStack module, PipeBlockEntity tile);
boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other); boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other);
boolean hasContainer(ItemStack module, PipeTileEntity tile); boolean hasContainer(ItemStack module, PipeBlockEntity tile);
AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex); AbstractPipeContainer<?> getContainer(ItemStack module, PipeBlockEntity tile, int windowId, Inventory inv, Player player, int moduleIndex);
float getItemSpeedIncrease(ItemStack module, PipeTileEntity tile); float getItemSpeedIncrease(ItemStack module, PipeBlockEntity tile);
boolean canPipeWork(ItemStack module, PipeTileEntity tile); boolean canPipeWork(ItemStack module, PipeBlockEntity tile);
List<ItemStack> getAllCraftables(ItemStack module, PipeTileEntity tile); List<ItemStack> getAllCraftables(ItemStack module, PipeBlockEntity tile);
int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain); int getCraftableAmount(ItemStack module, PipeBlockEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain);
ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain); ItemStack craft(ItemStack module, PipeBlockEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain);
Integer getCustomNextNode(ItemStack module, PipeTileEntity tile, List<BlockPos> nodes, int index); Integer getCustomNextNode(ItemStack module, PipeBlockEntity tile, List<BlockPos> nodes, int index);
ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile); ItemFilter getItemFilter(ItemStack module, PipeBlockEntity tile);
} }

View file

@ -3,16 +3,16 @@ package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.client.util.ITooltipFlag; import net.minecraft.core.BlockPos;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.chat.Component;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.item.Item; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.item.TooltipFlag;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.level.Level;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
@ -28,79 +28,79 @@ public abstract class ModuleItem extends Item implements IModule {
private final String name; private final String name;
public ModuleItem(String name) { public ModuleItem(String name) {
super(new Properties().group(Registry.GROUP).maxStackSize(16)); super(new Properties().tab(Registry.TAB).stacksTo(16));
this.name = name; this.name = name;
} }
@Override @Override
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) { public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List<Component> tooltip, TooltipFlag flagIn) {
super.addInformation(stack, worldIn, tooltip, flagIn); super.appendHoverText(stack, worldIn, tooltip, flagIn);
Utility.addTooltip(this.name, tooltip); Utility.addTooltip(this.name, tooltip);
} }
@Override @Override
public void tick(ItemStack module, PipeTileEntity tile) { public void tick(ItemStack module, PipeBlockEntity tile) {
} }
@Override @Override
public boolean canNetworkSee(ItemStack module, PipeTileEntity tile) { public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile) {
return true; return true;
} }
@Override @Override
public boolean canAcceptItem(ItemStack module, PipeTileEntity tile, ItemStack stack) { public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack) {
return true; return true;
} }
@Override @Override
public int getMaxInsertionAmount(ItemStack module, PipeTileEntity tile, ItemStack stack, IItemHandler destination) { public int getMaxInsertionAmount(ItemStack module, PipeBlockEntity tile, ItemStack stack, IItemHandler destination) {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
@Override @Override
public int getPriority(ItemStack module, PipeTileEntity tile) { public int getPriority(ItemStack module, PipeBlockEntity tile) {
return 0; return 0;
} }
@Override @Override
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) { public AbstractPipeContainer<?> getContainer(ItemStack module, PipeBlockEntity tile, int windowId, Inventory inv, Player player, int moduleIndex) {
return null; return null;
} }
@Override @Override
public float getItemSpeedIncrease(ItemStack module, PipeTileEntity tile) { public float getItemSpeedIncrease(ItemStack module, PipeBlockEntity tile) {
return 0; return 0;
} }
@Override @Override
public boolean canPipeWork(ItemStack module, PipeTileEntity tile) { public boolean canPipeWork(ItemStack module, PipeBlockEntity tile) {
return true; return true;
} }
@Override @Override
public List<ItemStack> getAllCraftables(ItemStack module, PipeTileEntity tile) { public List<ItemStack> getAllCraftables(ItemStack module, PipeBlockEntity tile) {
return Collections.emptyList(); return Collections.emptyList();
} }
@Override @Override
public int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) { public int getCraftableAmount(ItemStack module, PipeBlockEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
return 0; return 0;
} }
@Override @Override
public ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) { public ItemStack craft(ItemStack module, PipeBlockEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
return stack; return stack;
} }
@Override @Override
public Integer getCustomNextNode(ItemStack module, PipeTileEntity tile, List<BlockPos> nodes, int index) { public Integer getCustomNextNode(ItemStack module, PipeBlockEntity tile, List<BlockPos> nodes, int index) {
return null; return null;
} }
@Override @Override
public ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile) { public ItemFilter getItemFilter(ItemStack module, PipeBlockEntity tile) {
return null; return null;
} }
} }

View file

@ -3,71 +3,68 @@ package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.entities.PipeFrameEntity; import de.ellpeck.prettypipes.entities.PipeFrameEntity;
import net.minecraft.block.BlockState; import net.minecraft.core.BlockPos;
import net.minecraft.client.util.ITooltipFlag; import net.minecraft.core.Direction;
import net.minecraft.entity.EntityType; import net.minecraft.network.chat.Component;
import net.minecraft.entity.item.HangingEntity; import net.minecraft.world.InteractionResult;
import net.minecraft.entity.item.ItemFrameEntity; import net.minecraft.world.entity.EntityType;
import net.minecraft.entity.item.PaintingEntity; import net.minecraft.world.entity.decoration.HangingEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.world.entity.player.Player;
import net.minecraft.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.item.ItemUseContext; import net.minecraft.world.item.TooltipFlag;
import net.minecraft.nbt.CompoundTag; import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.util.ActionResultType; import net.minecraft.world.level.Level;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.World;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.List; import java.util.List;
public class PipeFrameItem extends Item { public class PipeFrameItem extends Item {
public PipeFrameItem() { public PipeFrameItem() {
super(new Properties().group(Registry.GROUP)); super(new Properties().tab(Registry.TAB));
} }
// HangingEntityItem copypasta mostly, since it hardcodes the entities bleh // HangingEntityItem copypasta mostly, since it hardcodes the entities bleh
@Override @Override
public ActionResultType onItemUse(ItemUseContext context) { public InteractionResult useOn(UseOnContext context) {
BlockPos blockpos = context.getPos(); var blockpos = context.getClickedPos();
Direction direction = context.getFace(); var direction = context.getClickedFace();
BlockPos blockpos1 = blockpos.offset(direction); var blockpos1 = blockpos.relative(direction);
PlayerEntity playerentity = context.getPlayer(); var playerentity = context.getPlayer();
ItemStack itemstack = context.getItem(); var itemstack = context.getItemInHand();
if (playerentity != null && !this.canPlace(playerentity, direction, itemstack, blockpos1)) { if (playerentity != null && !this.canPlace(playerentity, direction, itemstack, blockpos1)) {
return ActionResultType.FAIL; return InteractionResult.FAIL;
} else { } else {
World world = context.getWorld(); var world = context.getLevel();
HangingEntity hangingentity = new PipeFrameEntity(Registry.pipeFrameEntity, world, blockpos1, direction); HangingEntity hangingentity = new PipeFrameEntity(Registry.pipeFrameEntity, world, blockpos1, direction);
CompoundTag CompoundTag = itemstack.getTag(); var compoundTag = itemstack.getTag();
if (CompoundTag != null) { if (compoundTag != null) {
EntityType.applyItemNBT(world, playerentity, hangingentity, CompoundTag); EntityType.updateCustomEntityTag(world, playerentity, hangingentity, compoundTag);
} }
if (hangingentity.onValidSurface()) { if (hangingentity.survives()) {
if (!world.isRemote) { if (!world.isClientSide) {
hangingentity.playPlaceSound(); hangingentity.playPlacementSound();
world.addEntity(hangingentity); world.addFreshEntity(hangingentity);
} }
itemstack.shrink(1); itemstack.shrink(1);
return ActionResultType.SUCCESS; return InteractionResult.SUCCESS;
} else { } else {
return ActionResultType.CONSUME; return InteractionResult.CONSUME;
} }
} }
} }
protected boolean canPlace(PlayerEntity playerIn, Direction directionIn, ItemStack itemStackIn, BlockPos posIn) { protected boolean canPlace(Player playerIn, Direction directionIn, ItemStack itemStackIn, BlockPos posIn) {
return !directionIn.getAxis().isVertical() && playerIn.canPlayerEdit(posIn, directionIn, itemStackIn) && PipeFrameEntity.canPlace(playerIn.world, posIn, directionIn); return !directionIn.getAxis().isVertical() && playerIn.mayUseItemAt(posIn, directionIn, itemStackIn) && PipeFrameEntity.canPlace(playerIn.level, posIn, directionIn);
} }
@Override @Override
public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) { public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List<Component> tooltip, TooltipFlag flagIn) {
super.addInformation(stack, worldIn, tooltip, flagIn); super.appendHoverText(stack, worldIn, tooltip, flagIn);
Utility.addTooltip(this.getRegistryName().getPath(), tooltip); Utility.addTooltip(this.getRegistryName().getPath(), tooltip);
} }
} }

View file

@ -4,48 +4,45 @@ import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.pipe.ConnectionType; import de.ellpeck.prettypipes.pipe.ConnectionType;
import de.ellpeck.prettypipes.pipe.PipeBlock; import de.ellpeck.prettypipes.pipe.PipeBlock;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.block.Block; import net.minecraft.network.chat.Component;
import net.minecraft.block.BlockState; import net.minecraft.sounds.SoundEvents;
import net.minecraft.client.util.ITooltipFlag; import net.minecraft.sounds.SoundSource;
import net.minecraft.enchantment.Enchantment; import net.minecraft.world.InteractionResult;
import net.minecraft.enchantment.Enchantments; import net.minecraft.world.item.BlockItem;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.world.item.Item;
import net.minecraft.item.*; import net.minecraft.world.item.ItemStack;
import net.minecraft.state.EnumProperty; import net.minecraft.world.item.TooltipFlag;
import net.minecraft.util.ActionResultType; import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.util.Direction; import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.util.SoundCategory; import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.util.SoundEvents; import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.world.level.Level;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.level.block.Block;
import net.minecraft.util.math.shapes.VoxelShape; import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.World;
import java.util.List; import java.util.List;
import java.util.Map;
public class WrenchItem extends Item { public class WrenchItem extends Item {
public WrenchItem() { public WrenchItem() {
super(new Item.Properties().maxStackSize(1).group(Registry.GROUP)); super(new Item.Properties().stacksTo(1).tab(Registry.TAB));
} }
@Override @Override
public ActionResultType onItemUse(ItemUseContext context) { public InteractionResult useOn(UseOnContext context) {
World world = context.getWorld(); var world = context.getLevel();
BlockPos pos = context.getPos(); var pos = context.getClickedPos();
PlayerEntity player = context.getPlayer(); var player = context.getPlayer();
BlockState state = world.getBlockState(pos); var state = world.getBlockState(pos);
if (!(state.getBlock() instanceof PipeBlock)) if (!(state.getBlock() instanceof PipeBlock))
return ActionResultType.PASS; return InteractionResult.PASS;
PipeTileEntity tile = Utility.getBlockEntity(PipeTileEntity.class, world, pos); var tile = Utility.getBlockEntity(PipeBlockEntity.class, world, pos);
if (tile == null) if (tile == null)
return ActionResultType.FAIL; return InteractionResult.FAIL;
if (player.isSneaking()) { if (player.isCrouching()) {
if (!world.isRemote) { if (!world.isClientSide) {
if (tile.cover != null) { if (tile.cover != null) {
// remove the cover // remove the cover
tile.removeCover(player, context.getHand()); tile.removeCover(player, context.getHand());
@ -53,64 +50,64 @@ public class WrenchItem extends Item {
} else { } else {
// remove the pipe // remove the pipe
PipeBlock.dropItems(world, pos, player); PipeBlock.dropItems(world, pos, player);
Block.spawnDrops(state, world, pos, tile, null, ItemStack.EMPTY); Block.dropResources(state, world, pos, tile, null, ItemStack.EMPTY);
world.removeBlock(pos, false); world.removeBlock(pos, false);
} }
world.playSound(null, pos, SoundEvents.ENTITY_ITEM_FRAME_REMOVE_ITEM, SoundCategory.PLAYERS, 1, 1); world.playSound(null, pos, SoundEvents.ITEM_FRAME_REMOVE_ITEM, SoundSource.PLAYERS, 1, 1);
} }
return ActionResultType.func_233537_a_(world.isRemote); return InteractionResult.sidedSuccess(world.isClientSide);
} }
// placing covers // placing covers
if (tile.cover == null) { if (tile.cover == null) {
ItemStack offhand = player.getHeldItemOffhand(); var offhand = player.getOffhandItem();
if (offhand.getItem() instanceof BlockItem) { if (offhand.getItem() instanceof BlockItem) {
if (!world.isRemote) { if (!world.isClientSide) {
BlockItemUseContext blockContext = new BlockItemUseContext(context); var blockContext = new BlockPlaceContext(context);
Block block = ((BlockItem) offhand.getItem()).getBlock(); var block = ((BlockItem) offhand.getItem()).getBlock();
BlockState cover = block.getStateForPlacement(blockContext); var cover = block.getStateForPlacement(blockContext);
if (cover != null && !block.hasTileEntity(cover)) { if (cover != null && !(block instanceof EntityBlock)) {
tile.cover = cover; tile.cover = cover;
Utility.sendBlockEntityToClients(tile); Utility.sendBlockEntityToClients(tile);
offhand.shrink(1); offhand.shrink(1);
world.playSound(null, pos, SoundEvents.ENTITY_ITEM_FRAME_ADD_ITEM, SoundCategory.PLAYERS, 1, 1); world.playSound(null, pos, SoundEvents.ITEM_FRAME_ADD_ITEM, SoundSource.PLAYERS, 1, 1);
} }
} }
return ActionResultType.func_233537_a_(world.isRemote); return InteractionResult.sidedSuccess(world.isClientSide);
} }
} }
// disabling directions // disabling directions
for (Map.Entry<Direction, VoxelShape> entry : PipeBlock.DIR_SHAPES.entrySet()) { for (var entry : PipeBlock.DIR_SHAPES.entrySet()) {
AxisAlignedBB box = entry.getValue().getBoundingBox().offset(pos).grow(0.001F); var box = entry.getValue().bounds().move(pos).inflate(0.001F);
if (!box.contains(context.getHitVec())) if (!box.contains(context.getClickLocation()))
continue; continue;
EnumProperty<ConnectionType> prop = PipeBlock.DIRECTIONS.get(entry.getKey()); var prop = PipeBlock.DIRECTIONS.get(entry.getKey());
ConnectionType curr = state.get(prop); var curr = state.getValue(prop);
if (curr == ConnectionType.DISCONNECTED) if (curr == ConnectionType.DISCONNECTED)
continue; continue;
if (!world.isRemote) { if (!world.isClientSide) {
ConnectionType newType = curr == ConnectionType.BLOCKED ? ConnectionType.CONNECTED : ConnectionType.BLOCKED; var newType = curr == ConnectionType.BLOCKED ? ConnectionType.CONNECTED : ConnectionType.BLOCKED;
BlockPos otherPos = pos.offset(entry.getKey()); var otherPos = pos.relative(entry.getKey());
BlockState otherState = world.getBlockState(otherPos); var otherState = world.getBlockState(otherPos);
if (otherState.getBlock() instanceof PipeBlock) { if (otherState.getBlock() instanceof PipeBlock) {
otherState = otherState.with(PipeBlock.DIRECTIONS.get(entry.getKey().getOpposite()), newType); otherState = otherState.setValue(PipeBlock.DIRECTIONS.get(entry.getKey().getOpposite()), newType);
world.setBlockState(otherPos, otherState); world.setBlockAndUpdate(otherPos, otherState);
PipeBlock.onStateChanged(world, otherPos, otherState); PipeBlock.onStateChanged(world, otherPos, otherState);
} }
BlockState newState = state.with(prop, newType); var newState = state.setValue(prop, newType);
world.setBlockState(pos, newState); world.setBlockAndUpdate(pos, newState);
PipeBlock.onStateChanged(world, pos, newState); PipeBlock.onStateChanged(world, pos, newState);
world.playSound(null, pos, SoundEvents.ENTITY_ITEM_FRAME_ROTATE_ITEM, SoundCategory.PLAYERS, 1, 1); world.playSound(null, pos, SoundEvents.ITEM_FRAME_ROTATE_ITEM, SoundSource.PLAYERS, 1, 1);
} }
return ActionResultType.func_233537_a_(world.isRemote); return InteractionResult.sidedSuccess(world.isClientSide);
} }
return ActionResultType.PASS; return InteractionResult.PASS;
} }
@Override @Override
public void addInformation(ItemStack stack, World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) { public void appendHoverText(ItemStack stack, Level worldIn, List<Component> tooltip, TooltipFlag flagIn) {
Utility.addTooltip(this.getRegistryName().getPath(), tooltip); Utility.addTooltip(this.getRegistryName().getPath(), tooltip);
} }

View file

@ -5,21 +5,11 @@ import net.minecraft.world.item.ItemStack;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
public class EquatableItemStack { public record EquatableItemStack(ItemStack stack, ItemEquality... equalityTypes) {
public final ItemStack stack;
public final ItemEquality[] equalityTypes;
public EquatableItemStack(ItemStack stack, ItemEquality... equalityTypes) {
this.stack = stack;
this.equalityTypes = equalityTypes;
}
public boolean equals(Object o) { public boolean equals(Object o) {
if (o instanceof EquatableItemStack) { if (o instanceof EquatableItemStack other)
EquatableItemStack other = (EquatableItemStack) o;
return Arrays.equals(this.equalityTypes, other.equalityTypes) && ItemEquality.compareItems(this.stack, other.stack, this.equalityTypes); return Arrays.equals(this.equalityTypes, other.equalityTypes) && ItemEquality.compareItems(this.stack, other.stack, this.equalityTypes);
}
return false; return false;
} }

View file

@ -3,7 +3,7 @@ package de.ellpeck.prettypipes.misc;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.World; import net.minecraft.world.level.Level;
import net.minecraftforge.event.AttachCapabilitiesEvent; import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod;
@ -12,7 +12,7 @@ import net.minecraftforge.fml.common.Mod;
public final class Events { public final class Events {
@SubscribeEvent @SubscribeEvent
public static void onWorldCaps(AttachCapabilitiesEvent<World> event) { public static void onWorldCaps(AttachCapabilitiesEvent<Level> event) {
event.addCapability(new ResourceLocation(PrettyPipes.ID, "network"), new PipeNetwork(event.getObject())); event.addCapability(new ResourceLocation(PrettyPipes.ID, "network"), new PipeNetwork(event.getObject()));
} }
} }

View file

@ -1,11 +1,11 @@
package de.ellpeck.prettypipes.misc; package de.ellpeck.prettypipes.misc;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.world.entity.player.Player;
import net.minecraft.inventory.container.Container; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.inventory.container.Slot;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler; import net.minecraftforge.items.SlotItemHandler;
import org.jetbrains.annotations.NotNull;
public class FilterSlot extends SlotItemHandler { public class FilterSlot extends SlotItemHandler {
@ -16,9 +16,9 @@ public class FilterSlot extends SlotItemHandler {
this.onlyOneItem = onlyOneItem; this.onlyOneItem = onlyOneItem;
} }
public static boolean checkFilter(Container container, int slotId, PlayerEntity player) { public static boolean checkFilter(AbstractContainerMenu container, int slotId, Player player) {
if (slotId >= 0 && slotId < container.inventorySlots.size()) { if (slotId >= 0 && slotId < container.slots.size()) {
Slot slot = container.getSlot(slotId); var slot = container.getSlot(slotId);
if (slot instanceof FilterSlot) { if (slot instanceof FilterSlot) {
((FilterSlot) slot).slotClick(player); ((FilterSlot) slot).slotClick(player);
return true; return true;
@ -27,32 +27,33 @@ public class FilterSlot extends SlotItemHandler {
return false; return false;
} }
private void slotClick(PlayerEntity player) { private void slotClick(Player player) {
ItemStack heldStack = player.inventory.getItemStack(); var heldStack = player.inventoryMenu.getCarried();
ItemStack stackInSlot = this.getStack(); var stackInSlot = this.getItem();
if (!stackInSlot.isEmpty() && heldStack.isEmpty()) { if (!stackInSlot.isEmpty() && heldStack.isEmpty()) {
this.putStack(ItemStack.EMPTY); this.safeInsert(ItemStack.EMPTY);
} else if (!heldStack.isEmpty()) { } else if (!heldStack.isEmpty()) {
ItemStack s = heldStack.copy(); var s = heldStack.copy();
if (this.onlyOneItem) if (this.onlyOneItem)
s.setCount(1); s.setCount(1);
this.putStack(s); this.safeInsert(s);
} }
} }
@Override @Override
public boolean isItemValid(ItemStack stack) { public boolean mayPlace(@NotNull ItemStack stack) {
return false; return false;
} }
@Override @Override
public void putStack(ItemStack stack) { public ItemStack safeInsert(ItemStack stack) {
super.putStack(stack.copy()); return super.safeInsert(stack.copy());
} }
@Override @Override
public boolean canTakeStack(PlayerEntity playerIn) { public boolean mayPickup(Player playerIn) {
return false; return false;
} }
} }

View file

@ -3,18 +3,19 @@ package de.ellpeck.prettypipes.misc;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.packets.PacketButton; import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.modules.modifier.FilterModifierModuleItem; import de.ellpeck.prettypipes.pipe.modules.modifier.FilterModifierModuleItem;
import net.minecraft.ChatFormatting; import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button; import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.Widget;
import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.Screen;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.inventory.Slot; import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler; import net.minecraftforge.items.ItemStackHandler;
@ -25,14 +26,14 @@ import java.util.function.Supplier;
public class ItemFilter extends ItemStackHandler { public class ItemFilter extends ItemStackHandler {
private final ItemStack stack; private final ItemStack stack;
private final PipeTileEntity pipe; private final PipeBlockEntity pipe;
public boolean isWhitelist; public boolean isWhitelist;
public boolean canPopulateFromInventories; public boolean canPopulateFromInventories;
public boolean canModifyWhitelist = true; public boolean canModifyWhitelist = true;
private boolean modified; private boolean modified;
public ItemFilter(int size, ItemStack stack, PipeTileEntity pipe) { public ItemFilter(int size, ItemStack stack, PipeBlockEntity pipe) {
super(size); super(size);
this.stack = stack; this.stack = stack;
this.pipe = pipe; this.pipe = pipe;
@ -42,23 +43,23 @@ public class ItemFilter extends ItemStackHandler {
public List<Slot> getSlots(int x, int y) { public List<Slot> getSlots(int x, int y) {
List<Slot> slots = new ArrayList<>(); List<Slot> slots = new ArrayList<>();
for (int i = 0; i < this.getSlots(); i++) for (var i = 0; i < this.getSlots(); i++)
slots.add(new FilterSlot(this, i, x + i % 9 * 18, y + i / 9 * 18, true)); slots.add(new FilterSlot(this, i, x + i % 9 * 18, y + i / 9 * 18, true));
return slots; return slots;
} }
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public List<Widget> getButtons(Screen gui, int x, int y) { public List<AbstractWidget> getButtons(Screen gui, int x, int y) {
List<Widget> buttons = new ArrayList<>(); List<AbstractWidget> buttons = new ArrayList<>();
if (this.canModifyWhitelist) { if (this.canModifyWhitelist) {
Supplier<TranslatableComponent> whitelistText = () -> new TranslatableComponent("info." + PrettyPipes.ID + "." + (this.isWhitelist ? "whitelist" : "blacklist")); var whitelistText = (Supplier<TranslatableComponent>) () -> new TranslatableComponent("info." + PrettyPipes.ID + "." + (this.isWhitelist ? "whitelist" : "blacklist"));
buttons.add(new Button(x, y, 70, 20, whitelistText.get(), button -> { buttons.add(new Button(x, y, 70, 20, whitelistText.get(), button -> {
PacketButton.sendAndExecute(this.pipe.getPos(), PacketButton.ButtonResult.FILTER_CHANGE, 0); PacketButton.sendAndExecute(this.pipe.getBlockPos(), PacketButton.ButtonResult.FILTER_CHANGE, 0);
button.setMessage(whitelistText.get()); button.setMessage(whitelistText.get());
})); }));
} }
if (this.canPopulateFromInventories) { if (this.canPopulateFromInventories) {
buttons.add(new Button(x + 72, y, 70, 20, new TranslatableComponent("info." + PrettyPipes.ID + ".populate"), button -> PacketButton.sendAndExecute(this.pipe.getPos(), PacketButton.ButtonResult.FILTER_CHANGE, 1)) { buttons.add(new Button(x + 72, y, 70, 20, new TranslatableComponent("info." + PrettyPipes.ID + ".populate"), button -> PacketButton.sendAndExecute(this.pipe.getBlockPos(), PacketButton.ButtonResult.FILTER_CHANGE, 1)) {
@Override @Override
public void renderToolTip(PoseStack matrix, int x, int y) { public void renderToolTip(PoseStack matrix, int x, int y) {
gui.renderTooltip(matrix, new TranslatableComponent("info." + PrettyPipes.ID + ".populate.description").withStyle(ChatFormatting.GRAY), x, y); gui.renderTooltip(matrix, new TranslatableComponent("info." + PrettyPipes.ID + ".populate.description").withStyle(ChatFormatting.GRAY), x, y);
@ -75,19 +76,19 @@ public class ItemFilter extends ItemStackHandler {
this.save(); this.save();
} else if (id == 1 && this.canPopulateFromInventories) { } else if (id == 1 && this.canPopulateFromInventories) {
// populate filter from inventories // populate filter from inventories
List<ItemFilter> filters = this.pipe.getFilters(); var filters = this.pipe.getFilters();
for (Direction direction : Direction.values()) { for (var direction : Direction.values()) {
IItemHandler handler = this.pipe.getItemHandler(direction); var handler = this.pipe.getItemHandler(direction);
if (handler == null) if (handler == null)
continue; continue;
for (int i = 0; i < handler.getSlots(); i++) { for (var i = 0; i < handler.getSlots(); i++) {
ItemStack stack = handler.getStackInSlot(i); var stack = handler.getStackInSlot(i);
if (stack.isEmpty() || this.isFiltered(stack)) if (stack.isEmpty() || this.isFiltered(stack))
continue; continue;
ItemStack copy = stack.copy(); var copy = stack.copy();
copy.setCount(1); copy.setCount(1);
// try inserting into ourselves and any filter increase modifiers // try inserting into ourselves and any filter increase modifiers
for (ItemFilter filter : filters) { for (var filter : filters) {
if (ItemHandlerHelper.insertItem(filter, copy, false).isEmpty()) { if (ItemHandlerHelper.insertItem(filter, copy, false).isEmpty()) {
filter.save(); filter.save();
break; break;
@ -103,11 +104,11 @@ public class ItemFilter extends ItemStackHandler {
} }
private boolean isFiltered(ItemStack stack) { private boolean isFiltered(ItemStack stack) {
ItemEquality[] types = getEqualityTypes(this.pipe); var types = getEqualityTypes(this.pipe);
// also check if any filter increase modules have the item we need // also check if any filter increase modules have the item we need
for (ItemStackHandler handler : this.pipe.getFilters()) { for (ItemStackHandler handler : this.pipe.getFilters()) {
for (int i = 0; i < handler.getSlots(); i++) { for (var i = 0; i < handler.getSlots(); i++) {
ItemStack filter = handler.getStackInSlot(i); var filter = handler.getStackInSlot(i);
if (filter.isEmpty()) if (filter.isEmpty())
continue; continue;
if (ItemEquality.compareItems(stack, filter, types)) if (ItemEquality.compareItems(stack, filter, types))
@ -126,7 +127,7 @@ public class ItemFilter extends ItemStackHandler {
@Override @Override
public CompoundTag serializeNBT() { public CompoundTag serializeNBT() {
CompoundTag nbt = super.serializeNBT(); var nbt = super.serializeNBT();
if (this.canModifyWhitelist) if (this.canModifyWhitelist)
nbt.putBoolean("whitelist", this.isWhitelist); nbt.putBoolean("whitelist", this.isWhitelist);
return nbt; return nbt;
@ -144,7 +145,7 @@ public class ItemFilter extends ItemStackHandler {
this.modified = true; this.modified = true;
} }
public static ItemEquality[] getEqualityTypes(PipeTileEntity pipe) { public static ItemEquality[] getEqualityTypes(PipeBlockEntity pipe) {
return pipe.streamModules() return pipe.streamModules()
.filter(m -> m.getRight() instanceof FilterModifierModuleItem) .filter(m -> m.getRight() instanceof FilterModifierModuleItem)
.map(m -> ((FilterModifierModuleItem) m.getRight()).getEqualityType(m.getLeft())) .map(m -> ((FilterModifierModuleItem) m.getRight()).getEqualityType(m.getLeft()))
@ -152,6 +153,7 @@ public class ItemFilter extends ItemStackHandler {
} }
public interface IFilteredContainer { public interface IFilteredContainer {
ItemFilter getFilter(); ItemFilter getFilter();
} }
} }

View file

@ -1,26 +1,17 @@
package de.ellpeck.prettypipes.misc; package de.ellpeck.prettypipes.misc;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
import de.ellpeck.prettypipes.PrettyPipes; import com.mojang.blaze3d.vertex.PoseStack;
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui; import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui;
import net.minecraft.client.Minecraft; import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.screen.inventory.ContainerScreen; import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.widget.Widget; import net.minecraft.network.chat.TextComponent;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.client.gui.GuiUtils;
import org.apache.commons.lang3.tuple.Pair;
import java.util.List; import java.util.Optional;
public class ItemTerminalWidget extends Widget { public class ItemTerminalWidget extends AbstractWidget {
private final ItemTerminalGui screen; private final ItemTerminalGui screen;
public final int gridX; public final int gridX;
@ -30,7 +21,7 @@ public class ItemTerminalWidget extends Widget {
public boolean craftable; public boolean craftable;
public ItemTerminalWidget(int xIn, int yIn, int gridX, int gridY, ItemTerminalGui screen) { public ItemTerminalWidget(int xIn, int yIn, int gridX, int gridY, ItemTerminalGui screen) {
super(xIn, yIn, 16, 16, new StringTextComponent("")); super(xIn, yIn, 16, 16, new TextComponent(""));
this.gridX = gridX; this.gridX = gridX;
this.gridY = gridY; this.gridY = gridY;
this.screen = screen; this.screen = screen;
@ -44,25 +35,25 @@ public class ItemTerminalWidget extends Widget {
} }
@Override @Override
public void renderButton(MatrixStack matrix, int mouseX, int mouseY, float partialTicks) { public void renderButton(PoseStack matrix, int mouseX, int mouseY, float partialTicks) {
Minecraft mc = this.screen.getMinecraft(); var mc = this.screen.getMinecraft();
ItemRenderer renderer = mc.getItemRenderer(); var renderer = mc.getItemRenderer();
this.setBlitOffset(100); this.setBlitOffset(100);
renderer.zLevel = 100; renderer.blitOffset = 100;
if (this.selected) if (this.selected)
fill(matrix, this.x, this.y, this.x + 16, this.y + 16, -2130706433); fill(matrix, this.x, this.y, this.x + 16, this.y + 16, -2130706433);
RenderSystem.enableDepthTest(); RenderSystem.enableDepthTest();
renderer.renderItemAndEffectIntoGUI(mc.player, this.stack, this.x, this.y); renderer.renderGuiItem(this.stack, this.x, this.y);
int amount = !this.craftable ? this.stack.getCount() : 0; var amount = !this.craftable ? this.stack.getCount() : 0;
String amountStrg = this.stack.getCount() >= 1000 ? amount / 1000 + "k" : String.valueOf(amount); var amountStrg = this.stack.getCount() >= 1000 ? amount / 1000 + "k" : String.valueOf(amount);
RenderSystem.pushMatrix(); matrix.pushPose();
RenderSystem.scalef(0.8F, 0.8F, 1); matrix.scale(0.8F, 0.8F, 1);
renderer.renderItemOverlayIntoGUI(mc.fontRenderer, this.stack, (int) (this.x / 0.8F) + 4, (int) (this.y / 0.8F) + 4, amountStrg); renderer.renderGuiItemDecorations(mc.font, this.stack, (int) (this.x / 0.8F) + 4, (int) (this.y / 0.8F) + 4, amountStrg);
RenderSystem.popMatrix(); matrix.popPose();
renderer.zLevel = 0; renderer.blitOffset = 0;
this.setBlitOffset(0); this.setBlitOffset(0);
if (this.isHovered()) { if (this.isHoveredOrFocused()) {
RenderSystem.disableDepthTest(); RenderSystem.disableDepthTest();
RenderSystem.colorMask(true, true, true, false); RenderSystem.colorMask(true, true, true, false);
this.fillGradient(matrix, this.x, this.y, this.x + 16, this.y + 16, -2130706433, -2130706433); this.fillGradient(matrix, this.x, this.y, this.x + 16, this.y + 16, -2130706433, -2130706433);
@ -72,18 +63,21 @@ public class ItemTerminalWidget extends Widget {
} }
@Override @Override
public void renderToolTip(MatrixStack matrix, int mouseX, int mouseY) { public void renderToolTip(PoseStack matrix, int mouseX, int mouseY) {
if (this.visible && this.isHovered()) { if (this.visible && this.isHoveredOrFocused()) {
GuiUtils.preItemToolTip(this.stack); var tooltip = this.screen.getTooltipFromItem(this.stack);
List<ITextComponent> tooltip = this.screen.getTooltipFromItem(this.stack);
if (this.stack.getCount() >= 1000) { if (this.stack.getCount() >= 1000) {
ITextComponent comp = tooltip.get(0); var comp = tooltip.get(0);
if (comp instanceof TextComponent) { if (comp instanceof TextComponent text) {
tooltip.set(0, ((TextComponent) comp).append(new StringTextComponent(" (" + this.stack.getCount() + ')').mergeStyle(TextFormatting.BOLD))); tooltip.set(0, text.append(new TextComponent(" (" + this.stack.getCount() + ')').withStyle(ChatFormatting.BOLD)));
} }
} }
this.screen.func_243308_b(matrix, tooltip, mouseX, mouseY); this.screen.renderTooltip(matrix, tooltip, Optional.empty(), mouseX, mouseY);
GuiUtils.postItemToolTip();
} }
} }
@Override
public void updateNarration(NarrationElementOutput output) {
this.defaultButtonNarrationText(output);
}
} }

View file

@ -4,9 +4,11 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity;
import java.io.*; import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class PlayerPrefs { public class PlayerPrefs {
@ -18,10 +20,10 @@ public class PlayerPrefs {
public boolean syncJei = true; public boolean syncJei = true;
public void save() { public void save() {
File file = getFile(); var file = getFile();
if (file.exists()) if (file.exists())
file.delete(); file.delete();
try (FileWriter writer = new FileWriter(file)) { try (var writer = new FileWriter(file)) {
GSON.toJson(this, writer); GSON.toJson(this, writer);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -30,9 +32,9 @@ public class PlayerPrefs {
public static PlayerPrefs get() { public static PlayerPrefs get() {
if (instance == null) { if (instance == null) {
File file = getFile(); var file = getFile();
if (file.exists()) { if (file.exists()) {
try (FileReader reader = new FileReader(file)) { try (var reader = new FileReader(file)) {
instance = GSON.fromJson(reader, PlayerPrefs.class); instance = GSON.fromJson(reader, PlayerPrefs.class);
return instance; return instance;
} catch (IOException e) { } catch (IOException e) {
@ -45,7 +47,7 @@ public class PlayerPrefs {
} }
private static File getFile() { private static File getFile() {
File location = Minecraft.getInstance().gameDir; var location = Minecraft.getInstance().gameDirectory;
return new File(location, PrettyPipes.ID + "prefs"); return new File(location, PrettyPipes.ID + "prefs");
} }
} }

View file

@ -32,9 +32,9 @@ public class NetworkEdge extends DefaultWeightedEdge implements INBTSerializable
@Override @Override
public CompoundTag serializeNBT() { public CompoundTag serializeNBT() {
CompoundTag nbt = new CompoundTag(); var nbt = new CompoundTag();
ListTag list = new ListTag(); var list = new ListTag();
for (BlockPos pos : this.pipes) for (var pos : this.pipes)
list.add(NbtUtils.writeBlockPos(pos)); list.add(NbtUtils.writeBlockPos(pos));
nbt.put("pipes", list); nbt.put("pipes", list);
return nbt; return nbt;
@ -43,8 +43,8 @@ public class NetworkEdge extends DefaultWeightedEdge implements INBTSerializable
@Override @Override
public void deserializeNBT(CompoundTag nbt) { public void deserializeNBT(CompoundTag nbt) {
this.pipes.clear(); this.pipes.clear();
ListTag list = nbt.getList("pipes", Tag.TAG_COMPOUND); var list = nbt.getList("pipes", Tag.TAG_COMPOUND);
for (int i = 0; i < list.size(); i++) for (var i = 0; i < list.size(); i++)
this.pipes.add(NbtUtils.readBlockPos(list.getCompound(i))); this.pipes.add(NbtUtils.readBlockPos(list.getCompound(i)));
} }
} }

View file

@ -26,7 +26,7 @@ public class NetworkItem {
} }
public ItemStack asStack() { public ItemStack asStack() {
ItemStack stack = this.item.stack.copy(); var stack = this.item.stack().copy();
stack.setCount(this.amount); stack.setCount(this.amount);
return stack; return stack;
} }

View file

@ -1,13 +1,13 @@
package de.ellpeck.prettypipes.network; package de.ellpeck.prettypipes.network;
import de.ellpeck.prettypipes.misc.ItemEquality; import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NBTUtil; import net.minecraft.world.level.Level;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
@ -33,7 +33,7 @@ public class NetworkLocation implements INBTSerializable<CompoundTag> {
this.deserializeNBT(nbt); this.deserializeNBT(nbt);
} }
public List<Integer> getStackSlots(World world, ItemStack stack, ItemEquality... equalityTypes) { public List<Integer> getStackSlots(Level world, ItemStack stack, ItemEquality... equalityTypes) {
if (this.isEmpty(world)) if (this.isEmpty(world))
return Collections.emptyList(); return Collections.emptyList();
return this.getItems(world).entrySet().stream() return this.getItems(world).entrySet().stream()
@ -41,7 +41,7 @@ public class NetworkLocation implements INBTSerializable<CompoundTag> {
.map(Map.Entry::getKey).collect(Collectors.toList()); .map(Map.Entry::getKey).collect(Collectors.toList());
} }
public int getItemAmount(World world, ItemStack stack, ItemEquality... equalityTypes) { public int getItemAmount(Level world, ItemStack stack, ItemEquality... equalityTypes) {
if (this.isEmpty(world)) if (this.isEmpty(world))
return 0; return 0;
return this.getItems(world).entrySet().stream() return this.getItems(world).entrySet().stream()
@ -49,12 +49,12 @@ public class NetworkLocation implements INBTSerializable<CompoundTag> {
.mapToInt(kv -> kv.getValue().getCount()).sum(); .mapToInt(kv -> kv.getValue().getCount()).sum();
} }
public Map<Integer, ItemStack> getItems(World world) { public Map<Integer, ItemStack> getItems(Level world) {
if (this.itemCache == null) { if (this.itemCache == null) {
IItemHandler handler = this.getItemHandler(world); var handler = this.getItemHandler(world);
if (handler != null) { if (handler != null) {
for (int i = 0; i < handler.getSlots(); i++) { for (var i = 0; i < handler.getSlots(); i++) {
ItemStack stack = handler.getStackInSlot(i); var stack = handler.getStackInSlot(i);
// check if the slot is accessible to us // check if the slot is accessible to us
if (stack.isEmpty()) if (stack.isEmpty())
continue; continue;
@ -68,40 +68,40 @@ public class NetworkLocation implements INBTSerializable<CompoundTag> {
return this.itemCache; return this.itemCache;
} }
public boolean canExtract(World world, int slot) { public boolean canExtract(Level world, int slot) {
IItemHandler handler = this.getItemHandler(world); var handler = this.getItemHandler(world);
return handler != null && !handler.extractItem(slot, 1, true).isEmpty(); return handler != null && !handler.extractItem(slot, 1, true).isEmpty();
} }
public IItemHandler getItemHandler(World world) { public IItemHandler getItemHandler(Level world) {
if (this.handlerCache == null) { if (this.handlerCache == null) {
PipeNetwork network = PipeNetwork.get(world); var network = PipeNetwork.get(world);
PipeTileEntity pipe = network.getPipe(this.pipePos); var pipe = network.getPipe(this.pipePos);
this.handlerCache = pipe.getItemHandler(this.direction); this.handlerCache = pipe.getItemHandler(this.direction);
} }
return this.handlerCache; return this.handlerCache;
} }
public boolean isEmpty(World world) { public boolean isEmpty(Level world) {
Map<Integer, ItemStack> items = this.getItems(world); var items = this.getItems(world);
return items == null || items.isEmpty(); return items == null || items.isEmpty();
} }
public BlockPos getPos() { public BlockPos getPos() {
return this.pipePos.offset(this.direction); return this.pipePos.relative(this.direction);
} }
@Override @Override
public CompoundTag serializeNBT() { public CompoundTag serializeNBT() {
CompoundTag nbt = new CompoundTag(); var nbt = new CompoundTag();
nbt.put("pipe_pos", NBTUtil.writeBlockPos(this.pipePos)); nbt.put("pipe_pos", NbtUtils.writeBlockPos(this.pipePos));
nbt.putInt("direction", this.direction.getIndex()); nbt.putInt("direction", this.direction.ordinal());
return nbt; return nbt;
} }
@Override @Override
public void deserializeNBT(CompoundTag nbt) { public void deserializeNBT(CompoundTag nbt) {
this.pipePos = NBTUtil.readBlockPos(nbt.getCompound("pipe_pos")); this.pipePos = NbtUtils.readBlockPos(nbt.getCompound("pipe_pos"));
this.direction = Direction.byIndex(nbt.getInt("direction")); this.direction = Direction.values()[(nbt.getInt("direction"))];
} }
} }

View file

@ -1,8 +1,7 @@
package de.ellpeck.prettypipes.network; package de.ellpeck.prettypipes.network;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag; import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.common.util.INBTSerializable;
import java.util.Objects; import java.util.Objects;
@ -27,26 +26,24 @@ public class NetworkLock implements INBTSerializable<CompoundTag> {
@Override @Override
public CompoundTag serializeNBT() { public CompoundTag serializeNBT() {
CompoundTag nbt = new CompoundTag(); var nbt = new CompoundTag();
nbt.putUniqueId("id", this.lockId); nbt.putUUID("id", this.lockId);
nbt.put("location", this.location.serializeNBT()); nbt.put("location", this.location.serializeNBT());
nbt.put("stack", this.stack.write(new CompoundTag())); nbt.put("stack", this.stack.save(new CompoundTag()));
return nbt; return nbt;
} }
@Override @Override
public void deserializeNBT(CompoundTag nbt) { public void deserializeNBT(CompoundTag nbt) {
this.lockId = nbt.getUniqueId("id"); this.lockId = nbt.getUUID("id");
this.location = new NetworkLocation(nbt.getCompound("location")); this.location = new NetworkLocation(nbt.getCompound("location"));
this.stack = ItemStack.read(nbt.getCompound("stack")); this.stack = ItemStack.of(nbt.getCompound("stack"));
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (o instanceof NetworkLock) { if (o instanceof NetworkLock that)
NetworkLock that = (NetworkLock) o;
return this.lockId.equals(that.lockId); return this.lockId.equals(that.lockId);
}
return false; return false;
} }

View file

@ -1,33 +1,29 @@
package de.ellpeck.prettypipes.network; package de.ellpeck.prettypipes.network;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.vertex.PoseStack;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.pipe.IPipeConnectable;
import de.ellpeck.prettypipes.pipe.IPipeItem; import de.ellpeck.prettypipes.pipe.IPipeItem;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.model.ItemCameraTransforms; import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.entity.item.ItemEntity; import net.minecraft.core.BlockPos;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NBTUtil; import net.minecraft.nbt.NbtUtils;
import net.minecraft.util.Direction; import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.Mth;
import net.minecraft.util.math.MathHelper; import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.util.math.vector.Vector3d; import net.minecraft.world.item.BlockItem;
import net.minecraft.world.World; import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemHandlerHelper;
import org.jgrapht.Graph;
import org.jgrapht.GraphPath; import org.jgrapht.GraphPath;
import java.util.ArrayList; import java.util.ArrayList;
@ -88,35 +84,35 @@ public class PipeItem implements IPipeItem {
// initialize position if new // initialize position if new
if (this.x == 0 && this.y == 0 && this.z == 0) { if (this.x == 0 && this.y == 0 && this.z == 0) {
this.x = MathHelper.lerp(0.5F, startInventory.getX(), this.currGoalPos.getX()) + 0.5F; this.x = Mth.lerp(0.5F, startInventory.getX(), this.currGoalPos.getX()) + 0.5F;
this.y = MathHelper.lerp(0.5F, startInventory.getY(), this.currGoalPos.getY()) + 0.5F; this.y = Mth.lerp(0.5F, startInventory.getY(), this.currGoalPos.getY()) + 0.5F;
this.z = MathHelper.lerp(0.5F, startInventory.getZ(), this.currGoalPos.getZ()) + 0.5F; this.z = Mth.lerp(0.5F, startInventory.getZ(), this.currGoalPos.getZ()) + 0.5F;
} }
} }
@Override @Override
public void updateInPipe(PipeTileEntity currPipe) { public void updateInPipe(PipeBlockEntity currPipe) {
// this prevents pipes being updated after one another // this prevents pipes being updated after one another
// causing an item that just switched to tick twice // causing an item that just switched to tick twice
long worldTick = currPipe.getWorld().getGameTime(); var worldTick = currPipe.getLevel().getGameTime();
if (this.lastWorldTick == worldTick) if (this.lastWorldTick == worldTick)
return; return;
this.lastWorldTick = worldTick; this.lastWorldTick = worldTick;
float motionLeft = this.speed; var motionLeft = this.speed;
while (motionLeft > 0) { while (motionLeft > 0) {
float currSpeed = Math.min(0.25F, motionLeft); var currSpeed = Math.min(0.25F, motionLeft);
motionLeft -= currSpeed; motionLeft -= currSpeed;
BlockPos myPos = new BlockPos(this.x, this.y, this.z); var myPos = new BlockPos(this.x, this.y, this.z);
if (!myPos.equals(currPipe.getPos()) && (currPipe.getPos().equals(this.getDestPipe()) || !myPos.equals(this.startInventory))) { if (!myPos.equals(currPipe.getBlockPos()) && (currPipe.getBlockPos().equals(this.getDestPipe()) || !myPos.equals(this.startInventory))) {
// we're done with the current pipe, so switch to the next one // we're done with the current pipe, so switch to the next one
currPipe.getItems().remove(this); currPipe.getItems().remove(this);
PipeTileEntity next = this.getNextTile(currPipe, true); var next = this.getNextTile(currPipe, true);
if (next == null) { if (next == null) {
if (!currPipe.getWorld().isRemote) { if (!currPipe.getLevel().isClientSide) {
if (currPipe.getPos().equals(this.getDestPipe())) { if (currPipe.getBlockPos().equals(this.getDestPipe())) {
// ..or store in our destination container if we reached our destination // ...or store in our destination container if we reached our destination
this.stack = this.store(currPipe); this.stack = this.store(currPipe);
if (!this.stack.isEmpty()) if (!this.stack.isEmpty())
this.onPathObstructed(currPipe, true); this.onPathObstructed(currPipe, true);
@ -130,29 +126,29 @@ public class PipeItem implements IPipeItem {
currPipe = next; currPipe = next;
} }
} else { } else {
double dist = Vector3d.copy(this.currGoalPos).squareDistanceTo(this.x - 0.5F, this.y - 0.5F, this.z - 0.5F); var dist = (this.currGoalPos).distSqr(this.x - 0.5F, this.y - 0.5F, this.z - 0.5F, false);
if (dist < currSpeed * currSpeed) { if (dist < currSpeed * currSpeed) {
// we're past the start of the pipe, so move to the center of the next pipe // we're past the start of the pipe, so move to the center of the next pipe
BlockPos nextPos; BlockPos nextPos;
PipeTileEntity next = this.getNextTile(currPipe, false); var next = this.getNextTile(currPipe, false);
if (next == null || next == currPipe) { if (next == null || next == currPipe) {
if (currPipe.getPos().equals(this.getDestPipe())) { if (currPipe.getBlockPos().equals(this.getDestPipe())) {
nextPos = this.destInventory; nextPos = this.destInventory;
} else { } else {
currPipe.getItems().remove(this); currPipe.getItems().remove(this);
if (!currPipe.getWorld().isRemote) if (!currPipe.getLevel().isClientSide)
this.onPathObstructed(currPipe, false); this.onPathObstructed(currPipe, false);
return; return;
} }
} else { } else {
nextPos = next.getPos(); nextPos = next.getBlockPos();
} }
float tolerance = 0.001F; var tolerance = 0.001F;
if (dist >= tolerance * tolerance) { if (dist >= tolerance * tolerance) {
// when going around corners, we want to move right up to the corner // when going around corners, we want to move right up to the corner
Vector3d motion = new Vector3d(this.x - this.lastX, this.y - this.lastY, this.z - this.lastZ); var motion = new Vec3(this.x - this.lastX, this.y - this.lastY, this.z - this.lastZ);
Vector3d diff = new Vector3d(nextPos.getX() + 0.5F - this.x, nextPos.getY() + 0.5F - this.y, nextPos.getZ() + 0.5F - this.z); var diff = new Vec3(nextPos.getX() + 0.5F - this.x, nextPos.getY() + 0.5F - this.y, nextPos.getZ() + 0.5F - this.z);
if (motion.crossProduct(diff).length() >= tolerance) { if (motion.cross(diff).length() >= tolerance) {
currSpeed = (float) Math.sqrt(dist); currSpeed = (float) Math.sqrt(dist);
} else { } else {
// we're not going around a corner, so continue // we're not going around a corner, so continue
@ -169,7 +165,7 @@ public class PipeItem implements IPipeItem {
this.lastY = this.y; this.lastY = this.y;
this.lastZ = this.z; this.lastZ = this.z;
Vector3d dist = new Vector3d(this.currGoalPos.getX() + 0.5F - this.x, this.currGoalPos.getY() + 0.5F - this.y, this.currGoalPos.getZ() + 0.5F - this.z); var dist = new Vec3(this.currGoalPos.getX() + 0.5F - this.x, this.currGoalPos.getY() + 0.5F - this.y, this.currGoalPos.getZ() + 0.5F - this.z);
dist = dist.normalize(); dist = dist.normalize();
this.x += dist.x * currSpeed; this.x += dist.x * currSpeed;
this.y += dist.y * currSpeed; this.y += dist.y * currSpeed;
@ -177,50 +173,50 @@ public class PipeItem implements IPipeItem {
} }
} }
protected void onPathObstructed(PipeTileEntity currPipe, boolean tryReturn) { protected void onPathObstructed(PipeBlockEntity currPipe, boolean tryReturn) {
if (currPipe.getWorld().isRemote) if (currPipe.getLevel().isClientSide)
return; return;
PipeNetwork network = PipeNetwork.get(currPipe.getWorld()); var network = PipeNetwork.get(currPipe.getLevel());
if (tryReturn) { if (tryReturn) {
// first time: we try to return to our input chest // first time: we try to return to our input chest
if (!this.retryOnObstruction && network.routeItemToLocation(currPipe.getPos(), this.destInventory, this.getStartPipe(), this.startInventory, this.stack, speed -> this)) { if (!this.retryOnObstruction && network.routeItemToLocation(currPipe.getBlockPos(), this.destInventory, this.getStartPipe(), this.startInventory, this.stack, speed -> this)) {
this.retryOnObstruction = true; this.retryOnObstruction = true;
return; return;
} }
// second time: we arrived at our input chest, it is full, so we try to find a different goal location // second time: we arrived at our input chest, it is full, so we try to find a different goal location
ItemStack remain = network.routeItem(currPipe.getPos(), this.destInventory, this.stack, (stack, speed) -> this, false); var remain = network.routeItem(currPipe.getBlockPos(), this.destInventory, this.stack, (stack, speed) -> this, false);
if (!remain.isEmpty()) if (!remain.isEmpty())
this.drop(currPipe.getWorld(), remain.copy()); this.drop(currPipe.getLevel(), remain.copy());
} else { } else {
// if all re-routing attempts fail, we drop // if all re-routing attempts fail, we drop
this.drop(currPipe.getWorld(), this.stack); this.drop(currPipe.getLevel(), this.stack);
} }
} }
@Override @Override
public void drop(World world, ItemStack stack) { public void drop(Level world, ItemStack stack) {
ItemEntity item = new ItemEntity(world, this.x, this.y, this.z, stack.copy()); var item = new ItemEntity(world, this.x, this.y, this.z, stack.copy());
item.world.addEntity(item); item.level.addFreshEntity(item);
} }
protected ItemStack store(PipeTileEntity currPipe) { protected ItemStack store(PipeBlockEntity currPipe) {
Direction dir = Utility.getDirectionFromOffset(this.destInventory, this.getDestPipe()); var dir = Utility.getDirectionFromOffset(this.destInventory, this.getDestPipe());
IPipeConnectable connectable = currPipe.getPipeConnectable(dir); var connectable = currPipe.getPipeConnectable(dir);
if (connectable != null) if (connectable != null)
return connectable.insertItem(currPipe.getPos(), dir, this.stack, false); return connectable.insertItem(currPipe.getBlockPos(), dir, this.stack, false);
IItemHandler handler = currPipe.getItemHandler(dir); var handler = currPipe.getItemHandler(dir);
if (handler != null) if (handler != null)
return ItemHandlerHelper.insertItemStacked(handler, this.stack, false); return ItemHandlerHelper.insertItemStacked(handler, this.stack, false);
return this.stack; return this.stack;
} }
protected PipeTileEntity getNextTile(PipeTileEntity currPipe, boolean progress) { protected PipeBlockEntity getNextTile(PipeBlockEntity currPipe, boolean progress) {
if (this.path.size() <= this.currentTile + 1) if (this.path.size() <= this.currentTile + 1)
return null; return null;
BlockPos pos = this.path.get(this.currentTile + 1); var pos = this.path.get(this.currentTile + 1);
if (progress) if (progress)
this.currentTile++; this.currentTile++;
PipeNetwork network = PipeNetwork.get(currPipe.getWorld()); var network = PipeNetwork.get(currPipe.getLevel());
return network.getPipe(pos); return network.getPipe(pos);
} }
@ -245,41 +241,41 @@ public class PipeItem implements IPipeItem {
@Override @Override
public CompoundTag serializeNBT() { public CompoundTag serializeNBT() {
CompoundTag nbt = new CompoundTag(); var nbt = new CompoundTag();
nbt.putString("type", this.type.toString()); nbt.putString("type", this.type.toString());
nbt.put("stack", this.stack.serializeNBT()); nbt.put("stack", this.stack.serializeNBT());
nbt.putFloat("speed", this.speed); nbt.putFloat("speed", this.speed);
nbt.put("start_inv", NBTUtil.writeBlockPos(this.startInventory)); nbt.put("start_inv", NbtUtils.writeBlockPos(this.startInventory));
nbt.put("dest_inv", NBTUtil.writeBlockPos(this.destInventory)); nbt.put("dest_inv", NbtUtils.writeBlockPos(this.destInventory));
nbt.put("curr_goal", NBTUtil.writeBlockPos(this.currGoalPos)); nbt.put("curr_goal", NbtUtils.writeBlockPos(this.currGoalPos));
nbt.putBoolean("drop_on_obstruction", this.retryOnObstruction); nbt.putBoolean("drop_on_obstruction", this.retryOnObstruction);
nbt.putInt("tile", this.currentTile); nbt.putInt("tile", this.currentTile);
nbt.putFloat("x", this.x); nbt.putFloat("x", this.x);
nbt.putFloat("y", this.y); nbt.putFloat("y", this.y);
nbt.putFloat("z", this.z); nbt.putFloat("z", this.z);
ListTag list = new ListTag(); var list = new ListTag();
for (BlockPos pos : this.path) for (var pos : this.path)
list.add(NBTUtil.writeBlockPos(pos)); list.add(NbtUtils.writeBlockPos(pos));
nbt.put("path", list); nbt.put("path", list);
return nbt; return nbt;
} }
@Override @Override
public void deserializeNBT(CompoundTag nbt) { public void deserializeNBT(CompoundTag nbt) {
this.stack = ItemStack.read(nbt.getCompound("stack")); this.stack = ItemStack.of(nbt.getCompound("stack"));
this.speed = nbt.getFloat("speed"); this.speed = nbt.getFloat("speed");
this.startInventory = NBTUtil.readBlockPos(nbt.getCompound("start_inv")); this.startInventory = NbtUtils.readBlockPos(nbt.getCompound("start_inv"));
this.destInventory = NBTUtil.readBlockPos(nbt.getCompound("dest_inv")); this.destInventory = NbtUtils.readBlockPos(nbt.getCompound("dest_inv"));
this.currGoalPos = NBTUtil.readBlockPos(nbt.getCompound("curr_goal")); this.currGoalPos = NbtUtils.readBlockPos(nbt.getCompound("curr_goal"));
this.retryOnObstruction = nbt.getBoolean("drop_on_obstruction"); this.retryOnObstruction = nbt.getBoolean("drop_on_obstruction");
this.currentTile = nbt.getInt("tile"); this.currentTile = nbt.getInt("tile");
this.x = nbt.getFloat("x"); this.x = nbt.getFloat("x");
this.y = nbt.getFloat("y"); this.y = nbt.getFloat("y");
this.z = nbt.getFloat("z"); this.z = nbt.getFloat("z");
this.path.clear(); this.path.clear();
ListTag list = nbt.getList("path", Constants.NBT.TAG_COMPOUND); var list = nbt.getList("path", Tag.TAG_COMPOUND);
for (int i = 0; i < list.size(); i++) for (var i = 0; i < list.size(); i++)
this.path.add(NBTUtil.readBlockPos(list.getCompound(i))); this.path.add(NbtUtils.readBlockPos(list.getCompound(i)));
} }
@Override @Override
@ -289,40 +285,40 @@ public class PipeItem implements IPipeItem {
@Override @Override
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public void render(PipeTileEntity tile, MatrixStack matrixStack, Random random, float partialTicks, int light, int overlay, IRenderTypeBuffer buffer) { public void render(PipeBlockEntity tile, PoseStack matrixStack, Random random, float partialTicks, int light, int overlay, MultiBufferSource source) {
matrixStack.translate( matrixStack.translate(
MathHelper.lerp(partialTicks, this.lastX, this.x), Mth.lerp(partialTicks, this.lastX, this.x),
MathHelper.lerp(partialTicks, this.lastY, this.y), Mth.lerp(partialTicks, this.lastY, this.y),
MathHelper.lerp(partialTicks, this.lastZ, this.z)); Mth.lerp(partialTicks, this.lastZ, this.z));
if (this.stack.getItem() instanceof BlockItem) { if (this.stack.getItem() instanceof BlockItem) {
float scale = 0.7F; var scale = 0.7F;
matrixStack.scale(scale, scale, scale); matrixStack.scale(scale, scale, scale);
matrixStack.translate(0, -0.2F, 0); matrixStack.translate(0, -0.2F, 0);
} else { } else {
float scale = 0.45F; var scale = 0.45F;
matrixStack.scale(scale, scale, scale); matrixStack.scale(scale, scale, scale);
matrixStack.translate(0, -0.1F, 0); matrixStack.translate(0, -0.1F, 0);
} }
random.setSeed(Item.getIdFromItem(this.stack.getItem()) + this.stack.getDamage()); random.setSeed(Item.getId(this.stack.getItem()) + this.stack.getDamageValue());
int amount = this.getModelCount(); var amount = this.getModelCount();
for (int i = 0; i < amount; i++) { for (var i = 0; i < amount; i++) {
matrixStack.push(); matrixStack.pushPose();
if (amount > 1) { if (amount > 1) {
matrixStack.translate( matrixStack.translate(
(random.nextFloat() * 2.0F - 1.0F) * 0.25F * 0.5F, (random.nextFloat() * 2.0F - 1.0F) * 0.25F * 0.5F,
(random.nextFloat() * 2.0F - 1.0F) * 0.25F * 0.5F, (random.nextFloat() * 2.0F - 1.0F) * 0.25F * 0.5F,
(random.nextFloat() * 2.0F - 1.0F) * 0.25F * 0.5F); (random.nextFloat() * 2.0F - 1.0F) * 0.25F * 0.5F);
} }
Minecraft.getInstance().getItemRenderer().renderItem(this.stack, ItemCameraTransforms.TransformType.GROUND, light, overlay, matrixStack, buffer); Minecraft.getInstance().getItemRenderer().renderStatic(this.stack, ItemTransforms.TransformType.GROUND, light, overlay, matrixStack, source, 0);
matrixStack.pop(); matrixStack.popPose();
} }
} }
protected int getModelCount() { protected int getModelCount() {
int i = 1; var i = 1;
if (this.stack.getCount() > 48) { if (this.stack.getCount() > 48) {
i = 5; i = 5;
} else if (this.stack.getCount() > 32) { } else if (this.stack.getCount() > 32) {
@ -336,32 +332,32 @@ public class PipeItem implements IPipeItem {
} }
protected static List<BlockPos> compilePath(GraphPath<BlockPos, NetworkEdge> path) { protected static List<BlockPos> compilePath(GraphPath<BlockPos, NetworkEdge> path) {
Graph<BlockPos, NetworkEdge> graph = path.getGraph(); var graph = path.getGraph();
List<BlockPos> ret = new ArrayList<>(); List<BlockPos> ret = new ArrayList<>();
List<BlockPos> nodes = path.getVertexList(); var nodes = path.getVertexList();
if (nodes.size() == 1) { if (nodes.size() == 1) {
// add the single pipe twice if there's only one // add the single pipe twice if there's only one
// this is a dirty hack but it works fine so eh // this is a dirty hack, but it works fine so eh
for (int i = 0; i < 2; i++) for (var i = 0; i < 2; i++)
ret.add(nodes.get(0)); ret.add(nodes.get(0));
return ret; return ret;
} }
for (int i = 0; i < nodes.size() - 1; i++) { for (var i = 0; i < nodes.size() - 1; i++) {
BlockPos first = nodes.get(i); var first = nodes.get(i);
BlockPos second = nodes.get(i + 1); var second = nodes.get(i + 1);
NetworkEdge edge = graph.getEdge(first, second); var edge = graph.getEdge(first, second);
Consumer<Integer> add = j -> { var add = (Consumer<Integer>) j -> {
BlockPos pos = edge.pipes.get(j); var pos = edge.pipes.get(j);
if (!ret.contains(pos)) if (!ret.contains(pos))
ret.add(pos); ret.add(pos);
}; };
// if the edge is the other way around, we need to loop through tiles // if the edge is the other way around, we need to loop through tiles
// the other way also // the other way also
if (!graph.getEdgeSource(edge).equals(first)) { if (!graph.getEdgeSource(edge).equals(first)) {
for (int j = edge.pipes.size() - 1; j >= 0; j--) for (var j = edge.pipes.size() - 1; j >= 0; j--)
add.accept(j); add.accept(j);
} else { } else {
for (int j = 0; j < edge.pipes.size(); j++) for (var j = 0; j < edge.pipes.size(); j++)
add.accept(j); add.accept(j);
} }
} }

View file

@ -11,29 +11,21 @@ import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketItemEnterPipe; import de.ellpeck.prettypipes.packets.PacketItemEnterPipe;
import de.ellpeck.prettypipes.pipe.IPipeItem; import de.ellpeck.prettypipes.pipe.IPipeItem;
import de.ellpeck.prettypipes.pipe.PipeBlock; import de.ellpeck.prettypipes.pipe.PipeBlock;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.block.BlockState;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.nbt.*;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NBTUtil; import net.minecraft.nbt.NbtUtils;
import net.minecraft.util.Direction; import net.minecraft.nbt.Tag;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.World;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable; import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandler;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.jgrapht.GraphPath;
import org.jgrapht.ListenableGraph; import org.jgrapht.ListenableGraph;
import org.jgrapht.alg.interfaces.ShortestPathAlgorithm;
import org.jgrapht.alg.shortestpath.DijkstraShortestPath; import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.event.GraphEdgeChangeEvent; import org.jgrapht.event.GraphEdgeChangeEvent;
import org.jgrapht.event.GraphListener; import org.jgrapht.event.GraphListener;
@ -56,7 +48,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
public final ListenableGraph<BlockPos, NetworkEdge> graph; public final ListenableGraph<BlockPos, NetworkEdge> graph;
private final DijkstraShortestPath<BlockPos, NetworkEdge> dijkstra; private final DijkstraShortestPath<BlockPos, NetworkEdge> dijkstra;
private final Map<BlockPos, List<BlockPos>> nodeToConnectedNodes = new HashMap<>(); private final Map<BlockPos, List<BlockPos>> nodeToConnectedNodes = new HashMap<>();
private final Map<BlockPos, PipeTileEntity> tileCache = new HashMap<>(); private final Map<BlockPos, PipeBlockEntity> tileCache = new HashMap<>();
private final ListMultimap<BlockPos, IPipeItem> pipeItems = ArrayListMultimap.create(); private final ListMultimap<BlockPos, IPipeItem> pipeItems = ArrayListMultimap.create();
private final ListMultimap<BlockPos, NetworkLock> networkLocks = ArrayListMultimap.create(); private final ListMultimap<BlockPos, NetworkLock> networkLocks = ArrayListMultimap.create();
private final Level world; private final Level world;
@ -77,13 +69,13 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
@Override @Override
public CompoundTag serializeNBT() { public CompoundTag serializeNBT() {
CompoundTag nbt = new CompoundTag(); var nbt = new CompoundTag();
ListTag nodes = new ListTag(); var nodes = new ListTag();
for (BlockPos node : this.graph.vertexSet()) for (var node : this.graph.vertexSet())
nodes.add(NbtUtils.writeBlockPos(node)); nodes.add(NbtUtils.writeBlockPos(node));
nbt.put("nodes", nodes); nbt.put("nodes", nodes);
ListTag edges = new ListTag(); var edges = new ListTag();
for (NetworkEdge edge : this.graph.edgeSet()) for (var edge : this.graph.edgeSet())
edges.add(edge.serializeNBT()); edges.add(edge.serializeNBT());
nbt.put("edges", edges); nbt.put("edges", edges);
nbt.put("items", Utility.serializeAll(this.pipeItems.values())); nbt.put("items", Utility.serializeAll(this.pipeItems.values()));
@ -97,15 +89,15 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
this.pipeItems.clear(); this.pipeItems.clear();
this.networkLocks.clear(); this.networkLocks.clear();
ListTag nodes = nbt.getList("nodes", Tag.TAG_COMPOUND); var nodes = nbt.getList("nodes", Tag.TAG_COMPOUND);
for (int i = 0; i < nodes.size(); i++) for (var i = 0; i < nodes.size(); i++)
this.graph.addVertex(NBTUtil.readBlockPos(nodes.getCompound(i))); this.graph.addVertex(NbtUtils.readBlockPos(nodes.getCompound(i)));
ListTag edges = nbt.getList("edges", Tag.TAG_COMPOUND); var edges = nbt.getList("edges", Tag.TAG_COMPOUND);
for (int i = 0; i < edges.size(); i++) for (var i = 0; i < edges.size(); i++)
this.addEdge(new NetworkEdge(edges.getCompound(i))); this.addEdge(new NetworkEdge(edges.getCompound(i)));
for (IPipeItem item : Utility.deserializeAll(nbt.getList("items", NBT.TAG_COMPOUND), IPipeItem::load)) for (var item : Utility.deserializeAll(nbt.getList("items", Tag.TAG_COMPOUND), IPipeItem::load))
this.pipeItems.put(item.getCurrentPipe(), item); this.pipeItems.put(item.getCurrentPipe(), item);
for (NetworkLock lock : Utility.deserializeAll(nbt.getList("locks", NBT.TAG_COMPOUND), NetworkLock::new)) for (var lock : Utility.deserializeAll(nbt.getList("locks", Tag.TAG_COMPOUND), NetworkLock::new))
this.createNetworkLock(lock); this.createNetworkLock(lock);
} }
@ -126,12 +118,12 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
} }
public void onPipeChanged(BlockPos pos, BlockState state) { public void onPipeChanged(BlockPos pos, BlockState state) {
List<NetworkEdge> neighbors = this.createAllEdges(pos, state, true); var neighbors = this.createAllEdges(pos, state, true);
// if we only have one neighbor, then there can't be any new connections // if we only have one neighbor, then there can't be any new connections
if (neighbors.size() <= 1 && !this.isNode(pos)) if (neighbors.size() <= 1 && !this.isNode(pos))
return; return;
for (NetworkEdge edge : neighbors) { for (var edge : neighbors) {
BlockPos end = edge.getEndPipe(); var end = edge.getEndPipe();
this.refreshNode(end, this.world.getBlockState(end)); this.refreshNode(end, this.world.getBlockState(end));
} }
} }
@ -143,24 +135,24 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
public ItemStack routeItem(BlockPos startPipePos, BlockPos startInventory, ItemStack stack, BiFunction<ItemStack, Float, IPipeItem> itemSupplier, boolean preventOversending) { public ItemStack routeItem(BlockPos startPipePos, BlockPos startInventory, ItemStack stack, BiFunction<ItemStack, Float, IPipeItem> itemSupplier, boolean preventOversending) {
if (!this.isNode(startPipePos)) if (!this.isNode(startPipePos))
return stack; return stack;
if (!this.world.isBlockLoaded(startPipePos)) if (!this.world.isLoaded(startPipePos))
return stack; return stack;
PipeTileEntity startPipe = this.getPipe(startPipePos); var startPipe = this.getPipe(startPipePos);
if (startPipe == null) if (startPipe == null)
return stack; return stack;
this.startProfile("find_destination"); this.startProfile("find_destination");
List<BlockPos> nodes = this.getOrderedNetworkNodes(startPipePos); var nodes = this.getOrderedNetworkNodes(startPipePos);
for (int i = 0; i < nodes.size(); i++) { for (var i = 0; i < nodes.size(); i++) {
BlockPos pipePos = nodes.get(startPipe.getNextNode(nodes, i)); var pipePos = nodes.get(startPipe.getNextNode(nodes, i));
if (!this.world.isBlockLoaded(pipePos)) if (!this.world.isLoaded(pipePos))
continue; continue;
PipeTileEntity pipe = this.getPipe(pipePos); var pipe = this.getPipe(pipePos);
Pair<BlockPos, ItemStack> dest = pipe.getAvailableDestination(stack, false, preventOversending); var dest = pipe.getAvailableDestination(stack, false, preventOversending);
if (dest == null || dest.getLeft().equals(startInventory)) if (dest == null || dest.getLeft().equals(startInventory))
continue; continue;
Function<Float, IPipeItem> sup = speed -> itemSupplier.apply(dest.getRight(), speed); var sup = (Function<Float, IPipeItem>) speed -> itemSupplier.apply(dest.getRight(), speed);
if (this.routeItemToLocation(startPipePos, startInventory, pipe.getPos(), dest.getLeft(), dest.getRight(), sup)) { if (this.routeItemToLocation(startPipePos, startInventory, pipe.getBlockPos(), dest.getLeft(), dest.getRight(), sup)) {
ItemStack remain = stack.copy(); var remain = stack.copy();
remain.shrink(dest.getRight().getCount()); remain.shrink(dest.getRight().getCount());
this.endProfile(); this.endProfile();
return remain; return remain;
@ -173,17 +165,17 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
public boolean routeItemToLocation(BlockPos startPipePos, BlockPos startInventory, BlockPos destPipePos, BlockPos destInventory, ItemStack stack, Function<Float, IPipeItem> itemSupplier) { public boolean routeItemToLocation(BlockPos startPipePos, BlockPos startInventory, BlockPos destPipePos, BlockPos destInventory, ItemStack stack, Function<Float, IPipeItem> itemSupplier) {
if (!this.isNode(startPipePos) || !this.isNode(destPipePos)) if (!this.isNode(startPipePos) || !this.isNode(destPipePos))
return false; return false;
if (!this.world.isBlockLoaded(startPipePos) || !this.world.isBlockLoaded(destPipePos)) if (!this.world.isLoaded(startPipePos) || !this.world.isLoaded(destPipePos))
return false; return false;
PipeTileEntity startPipe = this.getPipe(startPipePos); var startPipe = this.getPipe(startPipePos);
if (startPipe == null) if (startPipe == null)
return false; return false;
this.startProfile("get_path"); this.startProfile("get_path");
GraphPath<BlockPos, NetworkEdge> path = this.dijkstra.getPath(startPipePos, destPipePos); var path = this.dijkstra.getPath(startPipePos, destPipePos);
this.endProfile(); this.endProfile();
if (path == null) if (path == null)
return false; return false;
IPipeItem item = itemSupplier.apply(startPipe.getItemSpeed(stack)); var item = itemSupplier.apply(startPipe.getItemSpeed(stack));
item.setDestination(startInventory, destInventory, path); item.setDestination(startInventory, destInventory, path);
startPipe.addNewItem(item); startPipe.addNewItem(item);
PacketHandler.sendToAllLoaded(this.world, startPipePos, new PacketItemEnterPipe(startPipePos, item)); PacketHandler.sendToAllLoaded(this.world, startPipePos, new PacketItemEnterPipe(startPipePos, item));
@ -191,9 +183,9 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
} }
public ItemStack requestItem(BlockPos destPipe, BlockPos destInventory, ItemStack stack, ItemEquality... equalityTypes) { public ItemStack requestItem(BlockPos destPipe, BlockPos destInventory, ItemStack stack, ItemEquality... equalityTypes) {
ItemStack remain = stack.copy(); var remain = stack.copy();
// check existing items // check existing items
for (NetworkLocation location : this.getOrderedNetworkItems(destPipe)) { for (var location : this.getOrderedNetworkItems(destPipe)) {
remain = this.requestExistingItem(location, destPipe, destInventory, null, remain, equalityTypes); remain = this.requestExistingItem(location, destPipe, destInventory, null, remain, equalityTypes);
if (remain.isEmpty()) if (remain.isEmpty())
return remain; return remain;
@ -203,10 +195,10 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
} }
public ItemStack requestCraftedItem(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) { public ItemStack requestCraftedItem(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) {
for (Pair<BlockPos, ItemStack> craftable : this.getAllCraftables(destPipe)) { for (var craftable : this.getAllCraftables(destPipe)) {
if (!ItemEquality.compareItems(stack, craftable.getRight(), equalityTypes)) if (!ItemEquality.compareItems(stack, craftable.getRight(), equalityTypes))
continue; continue;
PipeTileEntity pipe = this.getPipe(craftable.getLeft()); var pipe = this.getPipe(craftable.getLeft());
if (pipe == null) if (pipe == null)
continue; continue;
stack = pipe.craft(destPipe, unavailableConsumer, stack, dependencyChain); stack = pipe.craft(destPipe, unavailableConsumer, stack, dependencyChain);
@ -224,21 +216,21 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
if (location.getPos().equals(destInventory)) if (location.getPos().equals(destInventory))
return stack; return stack;
// make sure we don't pull any locked items // make sure we don't pull any locked items
int amount = location.getItemAmount(this.world, stack, equalityTypes); var amount = location.getItemAmount(this.world, stack, equalityTypes);
if (amount <= 0) if (amount <= 0)
return stack; return stack;
amount -= this.getLockedAmount(location.getPos(), stack, ignoredLock, equalityTypes); amount -= this.getLockedAmount(location.getPos(), stack, ignoredLock, equalityTypes);
if (amount <= 0) if (amount <= 0)
return stack; return stack;
ItemStack remain = stack.copy(); var remain = stack.copy();
// make sure we only extract less than or equal to the requested amount // make sure we only extract less than or equal to the requested amount
if (remain.getCount() < amount) if (remain.getCount() < amount)
amount = remain.getCount(); amount = remain.getCount();
remain.shrink(amount); remain.shrink(amount);
for (int slot : location.getStackSlots(this.world, stack, equalityTypes)) { for (int slot : location.getStackSlots(this.world, stack, equalityTypes)) {
// try to extract from that location's inventory and send the item // try to extract from that location's inventory and send the item
IItemHandler handler = location.getItemHandler(this.world); var handler = location.getItemHandler(this.world);
ItemStack extracted = handler.extractItem(slot, amount, true); var extracted = handler.extractItem(slot, amount, true);
if (this.routeItemToLocation(location.pipePos, location.getPos(), destPipe, destInventory, extracted, speed -> itemSupplier.apply(extracted, speed))) { if (this.routeItemToLocation(location.pipePos, location.getPos(), destPipe, destInventory, extracted, speed -> itemSupplier.apply(extracted, speed))) {
handler.extractItem(slot, extracted.getCount(), false); handler.extractItem(slot, extracted.getCount(), false);
amount -= extracted.getCount(); amount -= extracted.getCount();
@ -249,10 +241,10 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
return remain; return remain;
} }
public PipeTileEntity getPipe(BlockPos pos) { public PipeBlockEntity getPipe(BlockPos pos) {
PipeTileEntity tile = this.tileCache.get(pos); var tile = this.tileCache.get(pos);
if (tile == null || tile.isRemoved()) { if (tile == null || tile.isRemoved()) {
tile = Utility.getBlockEntity(PipeTileEntity.class, this.world, pos); tile = Utility.getBlockEntity(PipeBlockEntity.class, this.world, pos);
this.tileCache.put(pos, tile); this.tileCache.put(pos, tile);
} }
return tile; return tile;
@ -265,14 +257,14 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
public List<Pair<BlockPos, ItemStack>> getCurrentlyCrafting(BlockPos node, ItemEquality... equalityTypes) { public List<Pair<BlockPos, ItemStack>> getCurrentlyCrafting(BlockPos node, ItemEquality... equalityTypes) {
this.startProfile("get_currently_crafting"); this.startProfile("get_currently_crafting");
List<Pair<BlockPos, ItemStack>> items = new ArrayList<>(); List<Pair<BlockPos, ItemStack>> items = new ArrayList<>();
Iterator<PipeTileEntity> craftingPipes = this.getAllCraftables(node).stream().map(c -> this.getPipe(c.getLeft())).distinct().iterator(); var craftingPipes = this.getAllCraftables(node).stream().map(c -> this.getPipe(c.getLeft())).distinct().iterator();
while (craftingPipes.hasNext()) { while (craftingPipes.hasNext()) {
PipeTileEntity pipe = craftingPipes.next(); var pipe = craftingPipes.next();
for (Pair<BlockPos, ItemStack> request : pipe.craftResultRequests) { for (var request : pipe.craftResultRequests) {
BlockPos dest = request.getLeft(); var dest = request.getLeft();
ItemStack stack = request.getRight(); var stack = request.getRight();
// add up all the items that should go to the same location // add up all the items that should go to the same location
Optional<Pair<BlockPos, ItemStack>> existing = items.stream() var existing = items.stream()
.filter(s -> s.getLeft().equals(dest) && ItemEquality.compareItems(s.getRight(), stack, equalityTypes)) .filter(s -> s.getLeft().equals(dest) && ItemEquality.compareItems(s.getRight(), stack, equalityTypes))
.findFirst(); .findFirst();
if (existing.isPresent()) { if (existing.isPresent()) {
@ -297,25 +289,25 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
return Collections.emptyList(); return Collections.emptyList();
this.startProfile("get_all_craftables"); this.startProfile("get_all_craftables");
List<Pair<BlockPos, ItemStack>> craftables = new ArrayList<>(); List<Pair<BlockPos, ItemStack>> craftables = new ArrayList<>();
for (BlockPos dest : this.getOrderedNetworkNodes(node)) { for (var dest : this.getOrderedNetworkNodes(node)) {
if (!this.world.isBlockLoaded(dest)) if (!this.world.isLoaded(dest))
continue; continue;
PipeTileEntity pipe = this.getPipe(dest); var pipe = this.getPipe(dest);
for (ItemStack stack : pipe.getAllCraftables()) for (var stack : pipe.getAllCraftables())
craftables.add(Pair.of(pipe.getPos(), stack)); craftables.add(Pair.of(pipe.getBlockPos(), stack));
} }
this.endProfile(); this.endProfile();
return craftables; return craftables;
} }
public int getCraftableAmount(BlockPos node, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) { public int getCraftableAmount(BlockPos node, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) {
int total = 0; var total = 0;
for (Pair<BlockPos, ItemStack> pair : this.getAllCraftables(node)) { for (var pair : this.getAllCraftables(node)) {
if (!ItemEquality.compareItems(pair.getRight(), stack, equalityTypes)) if (!ItemEquality.compareItems(pair.getRight(), stack, equalityTypes))
continue; continue;
if (!this.world.isBlockLoaded(pair.getLeft())) if (!this.world.isLoaded(pair.getLeft()))
continue; continue;
PipeTileEntity pipe = this.getPipe(pair.getLeft()); var pipe = this.getPipe(pair.getLeft());
if (pipe != null) if (pipe != null)
total += pipe.getCraftableAmount(unavailableConsumer, stack, dependencyChain); total += pipe.getCraftableAmount(unavailableConsumer, stack, dependencyChain);
} }
@ -327,20 +319,20 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
return Collections.emptyList(); return Collections.emptyList();
this.startProfile("get_network_items"); this.startProfile("get_network_items");
List<NetworkLocation> info = new ArrayList<>(); List<NetworkLocation> info = new ArrayList<>();
for (BlockPos dest : this.getOrderedNetworkNodes(node)) { for (var dest : this.getOrderedNetworkNodes(node)) {
if (!this.world.isBlockLoaded(dest)) if (!this.world.isLoaded(dest))
continue; continue;
PipeTileEntity pipe = this.getPipe(dest); var pipe = this.getPipe(dest);
if (!pipe.canNetworkSee()) if (!pipe.canNetworkSee())
continue; continue;
for (Direction dir : Direction.values()) { for (var dir : Direction.values()) {
IItemHandler handler = pipe.getItemHandler(dir); var handler = pipe.getItemHandler(dir);
if (handler == null) if (handler == null)
continue; continue;
// check if this handler already exists (double-connected pipes, double chests etc.) // check if this handler already exists (double-connected pipes, double chests etc.)
if (info.stream().anyMatch(l -> handler.equals(l.getItemHandler(this.world)))) if (info.stream().anyMatch(l -> handler.equals(l.getItemHandler(this.world))))
continue; continue;
NetworkLocation location = new NetworkLocation(dest, dir); var location = new NetworkLocation(dest, dir);
if (!location.isEmpty(this.world)) if (!location.isEmpty(this.world))
info.add(location); info.add(location);
} }
@ -370,7 +362,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
private void refreshNode(BlockPos pos, BlockState state) { private void refreshNode(BlockPos pos, BlockState state) {
this.startProfile("refresh_node"); this.startProfile("refresh_node");
this.graph.removeAllEdges(new ArrayList<>(this.graph.edgesOf(pos))); this.graph.removeAllEdges(new ArrayList<>(this.graph.edgesOf(pos)));
for (NetworkEdge edge : this.createAllEdges(pos, state, false)) for (var edge : this.createAllEdges(pos, state, false))
this.addEdge(edge); this.addEdge(edge);
this.endProfile(); this.endProfile();
} }
@ -384,11 +376,11 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
public BlockPos getNodeFromPipe(BlockPos pos) { public BlockPos getNodeFromPipe(BlockPos pos) {
if (this.isNode(pos)) if (this.isNode(pos))
return pos; return pos;
BlockState state = this.world.getBlockState(pos); var state = this.world.getBlockState(pos);
if (!(state.getBlock() instanceof PipeBlock)) if (!(state.getBlock() instanceof PipeBlock))
return null; return null;
for (Direction dir : Direction.values()) { for (var dir : Direction.values()) {
NetworkEdge edge = this.createEdge(pos, state, dir, false); var edge = this.createEdge(pos, state, dir, false);
if (edge != null) if (edge != null)
return edge.getEndPipe(); return edge.getEndPipe();
} }
@ -398,8 +390,8 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
private List<NetworkEdge> createAllEdges(BlockPos pos, BlockState state, boolean ignoreCurrBlocked) { private List<NetworkEdge> createAllEdges(BlockPos pos, BlockState state, boolean ignoreCurrBlocked) {
this.startProfile("create_all_edges"); this.startProfile("create_all_edges");
List<NetworkEdge> edges = new ArrayList<>(); List<NetworkEdge> edges = new ArrayList<>();
for (Direction dir : Direction.values()) { for (var dir : Direction.values()) {
NetworkEdge edge = this.createEdge(pos, state, dir, ignoreCurrBlocked); var edge = this.createEdge(pos, state, dir, ignoreCurrBlocked);
if (edge != null) if (edge != null)
edges.add(edge); edges.add(edge);
} }
@ -408,14 +400,14 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
} }
private NetworkEdge createEdge(BlockPos pos, BlockState state, Direction dir, boolean ignoreCurrBlocked) { private NetworkEdge createEdge(BlockPos pos, BlockState state, Direction dir, boolean ignoreCurrBlocked) {
if (!ignoreCurrBlocked && !state.get(PipeBlock.DIRECTIONS.get(dir)).isConnected()) if (!ignoreCurrBlocked && !state.getValue(PipeBlock.DIRECTIONS.get(dir)).isConnected())
return null; return null;
BlockPos currPos = pos.offset(dir); var currPos = pos.relative(dir);
BlockState currState = this.world.getBlockState(currPos); var currState = this.world.getBlockState(currPos);
if (!(currState.getBlock() instanceof PipeBlock)) if (!(currState.getBlock() instanceof PipeBlock))
return null; return null;
this.startProfile("create_edge"); this.startProfile("create_edge");
NetworkEdge edge = new NetworkEdge(); var edge = new NetworkEdge();
edge.pipes.add(pos); edge.pipes.add(pos);
edge.pipes.add(currPos); edge.pipes.add(currPos);
@ -427,12 +419,12 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
return edge; return edge;
} }
boolean found = false; var found = false;
for (Direction nextDir : Direction.values()) { for (var nextDir : Direction.values()) {
if (!currState.get(PipeBlock.DIRECTIONS.get(nextDir)).isConnected()) if (!currState.getValue(PipeBlock.DIRECTIONS.get(nextDir)).isConnected())
continue; continue;
BlockPos offset = currPos.offset(nextDir); var offset = currPos.relative(nextDir);
BlockState offState = this.world.getBlockState(offset); var offState = this.world.getBlockState(offset);
if (!(offState.getBlock() instanceof PipeBlock)) if (!(offState.getBlock() instanceof PipeBlock))
continue; continue;
if (edge.pipes.contains(offset)) if (edge.pipes.contains(offset))
@ -453,10 +445,10 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
public List<BlockPos> getOrderedNetworkNodes(BlockPos node) { public List<BlockPos> getOrderedNetworkNodes(BlockPos node) {
if (!this.isNode(node)) if (!this.isNode(node))
return Collections.emptyList(); return Collections.emptyList();
List<BlockPos> ret = this.nodeToConnectedNodes.get(node); var ret = this.nodeToConnectedNodes.get(node);
if (ret == null) { if (ret == null) {
this.startProfile("compile_connected_nodes"); this.startProfile("compile_connected_nodes");
ShortestPathAlgorithm.SingleSourcePaths<BlockPos, NetworkEdge> paths = this.dijkstra.getPaths(node); var paths = this.dijkstra.getPaths(node);
// sort destinations first by their priority (eg trash pipes should be last) // sort destinations first by their priority (eg trash pipes should be last)
// and then by their distance from the specified node // and then by their distance from the specified node
ret = Streams.stream(new BreadthFirstIterator<>(this.graph, node)) ret = Streams.stream(new BreadthFirstIterator<>(this.graph, node))
@ -472,7 +464,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
public void clearDestinationCache(BlockPos... nodes) { public void clearDestinationCache(BlockPos... nodes) {
this.startProfile("clear_node_cache"); this.startProfile("clear_node_cache");
// remove caches for the nodes // remove caches for the nodes
for (BlockPos node : nodes) for (var node : nodes)
this.nodeToConnectedNodes.keySet().remove(node); this.nodeToConnectedNodes.keySet().remove(node);
// remove caches that contain the nodes as a destination // remove caches that contain the nodes as a destination
this.nodeToConnectedNodes.values().removeIf(cached -> Arrays.stream(nodes).anyMatch(cached::contains)); this.nodeToConnectedNodes.values().removeIf(cached -> Arrays.stream(nodes).anyMatch(cached::contains));
@ -485,7 +477,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
public Stream<IPipeItem> getPipeItemsOnTheWay(BlockPos goalInv) { public Stream<IPipeItem> getPipeItemsOnTheWay(BlockPos goalInv) {
this.startProfile("get_pipe_items_on_the_way"); this.startProfile("get_pipe_items_on_the_way");
Stream<IPipeItem> ret = this.pipeItems.values().stream().filter(i -> i.getDestInventory().equals(goalInv)); var ret = this.pipeItems.values().stream().filter(i -> i.getDestInventory().equals(goalInv));
this.endProfile(); this.endProfile();
return ret; return ret;
} }
@ -515,11 +507,11 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
} }
public void startProfile(String name) { public void startProfile(String name) {
this.world.getProfiler().startSection(() -> PrettyPipes.ID + ":pipe_network_" + name); this.world.getProfiler().push(() -> PrettyPipes.ID + ":pipe_network_" + name);
} }
public void endProfile() { public void endProfile() {
this.world.getProfiler().endSection(); this.world.getProfiler().pop();
} }
public static PipeNetwork get(Level world) { public static PipeNetwork get(Level world) {

View file

@ -2,27 +2,25 @@ package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer; import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import de.ellpeck.prettypipes.pipe.modules.modifier.FilterModifierModuleContainer; import de.ellpeck.prettypipes.pipe.modules.modifier.FilterModifierModuleContainer;
import de.ellpeck.prettypipes.pipe.modules.modifier.FilterModifierModuleItem; import de.ellpeck.prettypipes.pipe.modules.modifier.FilterModifierModuleItem;
import de.ellpeck.prettypipes.pipe.modules.stacksize.StackSizeModuleItem; import de.ellpeck.prettypipes.pipe.modules.stacksize.StackSizeModuleItem;
import de.ellpeck.prettypipes.terminal.CraftingTerminalTileEntity; import de.ellpeck.prettypipes.terminal.CraftingTerminalBlockEntity;
import de.ellpeck.prettypipes.terminal.ItemTerminalTileEntity; import de.ellpeck.prettypipes.terminal.ItemTerminalBlockEntity;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.network.chat.Component;
import net.minecraft.inventory.container.Container; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.inventory.container.INamedContainerProvider; import net.minecraft.world.MenuProvider;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.network.PacketBuffer; import net.minecraft.world.entity.player.Player;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.util.text.ITextComponent; import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.fml.network.NetworkEvent; import net.minecraftforge.network.NetworkHooks;
import net.minecraftforge.fml.network.NetworkHooks;
import org.apache.logging.log4j.util.TriConsumer; import org.apache.logging.log4j.util.TriConsumer;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -44,15 +42,15 @@ public class PacketButton {
} }
public static PacketButton fromBytes(PacketBuffer buf) { public static PacketButton fromBytes(FriendlyByteBuf buf) {
PacketButton packet = new PacketButton(); var packet = new PacketButton();
packet.pos = buf.readBlockPos(); packet.pos = buf.readBlockPos();
packet.result = ButtonResult.values()[buf.readByte()]; packet.result = ButtonResult.values()[buf.readByte()];
packet.data = buf.readVarIntArray(); packet.data = buf.readVarIntArray();
return packet; return packet;
} }
public static void toBytes(PacketButton packet, PacketBuffer buf) { public static void toBytes(PacketButton packet, FriendlyByteBuf buf) {
buf.writeBlockPos(packet.pos); buf.writeBlockPos(packet.pos);
buf.writeByte(packet.result.ordinal()); buf.writeByte(packet.result.ordinal());
buf.writeVarIntArray(packet.data); buf.writeVarIntArray(packet.data);
@ -63,7 +61,7 @@ public class PacketButton {
ctx.get().enqueueWork(new Runnable() { ctx.get().enqueueWork(new Runnable() {
@Override @Override
public void run() { public void run() {
PlayerEntity player = ctx.get().getSender(); Player player = ctx.get().getSender();
message.result.action.accept(message.pos, message.data, player); message.result.action.accept(message.pos, message.data, player);
} }
}); });
@ -77,22 +75,23 @@ public class PacketButton {
public enum ButtonResult { public enum ButtonResult {
PIPE_TAB((pos, data, player) -> { PIPE_TAB((pos, data, player) -> {
PipeTileEntity tile = Utility.getBlockEntity(PipeTileEntity.class, player.world, pos); var tile = Utility.getBlockEntity(PipeBlockEntity.class, player.level, pos);
if (data[0] < 0) { if (data[0] < 0) {
NetworkHooks.openGui((ServerPlayerEntity) player, tile, pos); NetworkHooks.openGui((ServerPlayer) player, tile, pos);
} else { } else {
ItemStack stack = tile.modules.getStackInSlot(data[0]); var stack = tile.modules.getStackInSlot(data[0]);
NetworkHooks.openGui((ServerPlayerEntity) player, new INamedContainerProvider() { NetworkHooks.openGui((ServerPlayer) player, new MenuProvider() {
@Override @Override
public ITextComponent getDisplayName() { public Component getDisplayName() {
return stack.getDisplayName(); return stack.getDisplayName();
} }
@Nullable @Nullable
@Override @Override
public Container createMenu(int windowId, PlayerInventory inv, PlayerEntity player) { public AbstractContainerMenu createMenu(int windowId, Inventory inv, Player player) {
return ((IModule) stack.getItem()).getContainer(stack, tile, windowId, inv, player, data[0]); return ((IModule) stack.getItem()).getContainer(stack, tile, windowId, inv, player, data[0]);
} }
}, buf -> { }, buf -> {
buf.writeBlockPos(pos); buf.writeBlockPos(pos);
buf.writeInt(data[0]); buf.writeInt(data[0]);
@ -100,34 +99,34 @@ public class PacketButton {
} }
}), }),
FILTER_CHANGE((pos, data, player) -> { FILTER_CHANGE((pos, data, player) -> {
IFilteredContainer container = (IFilteredContainer) player.openContainer; var container = (IFilteredContainer) player.containerMenu;
ItemFilter filter = container.getFilter(); var filter = container.getFilter();
filter.onButtonPacket(data[0]); filter.onButtonPacket(data[0]);
}), }),
STACK_SIZE_MODULE_BUTTON((pos, data, player) -> { STACK_SIZE_MODULE_BUTTON((pos, data, player) -> {
AbstractPipeContainer<?> container = (AbstractPipeContainer<?>) player.openContainer; var container = (AbstractPipeContainer<?>) player.containerMenu;
StackSizeModuleItem.setLimitToMaxStackSize(container.moduleStack, !StackSizeModuleItem.getLimitToMaxStackSize(container.moduleStack)); StackSizeModuleItem.setLimitToMaxStackSize(container.moduleStack, !StackSizeModuleItem.getLimitToMaxStackSize(container.moduleStack));
}), }),
STACK_SIZE_AMOUNT((pos, data, player) -> { STACK_SIZE_AMOUNT((pos, data, player) -> {
AbstractPipeContainer<?> container = (AbstractPipeContainer<?>) player.openContainer; var container = (AbstractPipeContainer<?>) player.containerMenu;
StackSizeModuleItem.setMaxStackSize(container.moduleStack, data[0]); StackSizeModuleItem.setMaxStackSize(container.moduleStack, data[0]);
}), }),
CRAFT_TERMINAL_REQUEST((pos, data, player) -> { CRAFT_TERMINAL_REQUEST((pos, data, player) -> {
CraftingTerminalTileEntity tile = Utility.getBlockEntity(CraftingTerminalTileEntity.class, player.world, pos); var tile = Utility.getBlockEntity(CraftingTerminalBlockEntity.class, player.level, pos);
tile.requestCraftingItems(player, data[0]); tile.requestCraftingItems(player, data[0]);
}), }),
CANCEL_CRAFTING((pos, data, player) -> { CANCEL_CRAFTING((pos, data, player) -> {
ItemTerminalTileEntity tile = Utility.getBlockEntity(ItemTerminalTileEntity.class, player.world, pos); var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, player.level, pos);
tile.cancelCrafting(); tile.cancelCrafting();
}), }),
TAG_FILTER((pos, data, player) -> { TAG_FILTER((pos, data, player) -> {
FilterModifierModuleContainer container = (FilterModifierModuleContainer) player.openContainer; var container = (FilterModifierModuleContainer) player.containerMenu;
FilterModifierModuleItem.setFilterTag(container.moduleStack, container.getTags().get(data[0])); FilterModifierModuleItem.setFilterTag(container.moduleStack, container.getTags().get(data[0]));
}); });
public final TriConsumer<BlockPos, int[], PlayerEntity> action; public final TriConsumer<BlockPos, int[], Player> action;
ButtonResult(TriConsumer<BlockPos, int[], PlayerEntity> action) { ButtonResult(TriConsumer<BlockPos, int[], Player> action) {
this.action = action; this.action = action;
} }
} }

View file

@ -1,12 +1,12 @@
package de.ellpeck.prettypipes.packets; package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleContainer; import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler; import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.network.NetworkEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -26,24 +26,24 @@ public class PacketCraftingModuleTransfer {
} }
public static PacketCraftingModuleTransfer fromBytes(PacketBuffer buf) { public static PacketCraftingModuleTransfer fromBytes(FriendlyByteBuf buf) {
PacketCraftingModuleTransfer packet = new PacketCraftingModuleTransfer(); var packet = new PacketCraftingModuleTransfer();
packet.inputs = new ArrayList<>(); packet.inputs = new ArrayList<>();
for (int i = buf.readInt(); i > 0; i--) for (var i = buf.readInt(); i > 0; i--)
packet.inputs.add(buf.readItemStack()); packet.inputs.add(buf.readItem());
packet.outputs = new ArrayList<>(); packet.outputs = new ArrayList<>();
for (int i = buf.readInt(); i > 0; i--) for (var i = buf.readInt(); i > 0; i--)
packet.outputs.add(buf.readItemStack()); packet.outputs.add(buf.readItem());
return packet; return packet;
} }
public static void toBytes(PacketCraftingModuleTransfer packet, PacketBuffer buf) { public static void toBytes(PacketCraftingModuleTransfer packet, FriendlyByteBuf buf) {
buf.writeInt(packet.inputs.size()); buf.writeInt(packet.inputs.size());
for (ItemStack stack : packet.inputs) for (var stack : packet.inputs)
buf.writeItemStack(stack); buf.writeItem(stack);
buf.writeInt(packet.outputs.size()); buf.writeInt(packet.outputs.size());
for (ItemStack stack : packet.outputs) for (var stack : packet.outputs)
buf.writeItemStack(stack); buf.writeItem(stack);
} }
@SuppressWarnings("Convert2Lambda") @SuppressWarnings("Convert2Lambda")
@ -51,13 +51,12 @@ public class PacketCraftingModuleTransfer {
ctx.get().enqueueWork(new Runnable() { ctx.get().enqueueWork(new Runnable() {
@Override @Override
public void run() { public void run() {
PlayerEntity player = ctx.get().getSender(); Player player = ctx.get().getSender();
if (player.openContainer instanceof CraftingModuleContainer) { if (player.containerMenu instanceof CraftingModuleContainer container) {
CraftingModuleContainer container = (CraftingModuleContainer) player.openContainer;
copy(container.input, message.inputs); copy(container.input, message.inputs);
copy(container.output, message.outputs); copy(container.output, message.outputs);
container.modified = true; container.modified = true;
container.detectAndSendChanges(); container.broadcastChanges();
} }
} }
}); });
@ -65,9 +64,9 @@ public class PacketCraftingModuleTransfer {
} }
private static void copy(ItemStackHandler container, List<ItemStack> contents) { private static void copy(ItemStackHandler container, List<ItemStack> contents) {
for (int i = 0; i < container.getSlots(); i++) for (var i = 0; i < container.getSlots(); i++)
container.setStackInSlot(i, ItemStack.EMPTY); container.setStackInSlot(i, ItemStack.EMPTY);
for (ItemStack stack : contents) for (var stack : contents)
ItemHandlerHelper.insertItem(container, stack, false); ItemHandlerHelper.insertItem(container, stack, false);
} }
} }

View file

@ -3,13 +3,13 @@ package de.ellpeck.prettypipes.packets;
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap; import com.google.common.collect.ListMultimap;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.terminal.CraftingTerminalTileEntity; import de.ellpeck.prettypipes.terminal.CraftingTerminalBlockEntity;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.network.PacketBuffer; import net.minecraftforge.network.NetworkEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.network.NetworkEvent;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -29,35 +29,35 @@ public class PacketGhostSlot {
} }
public static PacketGhostSlot fromBytes(PacketBuffer buf) { public static PacketGhostSlot fromBytes(FriendlyByteBuf buf) {
PacketGhostSlot packet = new PacketGhostSlot(); var packet = new PacketGhostSlot();
packet.pos = buf.readBlockPos(); packet.pos = buf.readBlockPos();
packet.stacks = ArrayListMultimap.create(); packet.stacks = ArrayListMultimap.create();
for (int i = buf.readInt(); i > 0; i--) for (var i = buf.readInt(); i > 0; i--)
packet.stacks.put(buf.readInt(), buf.readItemStack()); packet.stacks.put(buf.readInt(), buf.readItem());
return packet; return packet;
} }
public static void toBytes(PacketGhostSlot packet, PacketBuffer buf) { public static void toBytes(PacketGhostSlot packet, FriendlyByteBuf buf) {
buf.writeBlockPos(packet.pos); buf.writeBlockPos(packet.pos);
buf.writeInt(packet.stacks.size()); buf.writeInt(packet.stacks.size());
for (Map.Entry<Integer, ItemStack> entry : packet.stacks.entries()) { for (var entry : packet.stacks.entries()) {
buf.writeInt(entry.getKey()); buf.writeInt(entry.getKey());
buf.writeItemStack(entry.getValue()); buf.writeItem(entry.getValue());
} }
} }
@SuppressWarnings("Convert2Lambda") @SuppressWarnings("Convert2Lambda")
public static void onMessage(PacketGhostSlot message, Supplier<NetworkEvent.Context> ctx) { public static void onMessage(PacketGhostSlot message, Supplier<NetworkEvent.Context> ctx) {
Consumer<PlayerEntity> doIt = p -> { var doIt = (Consumer<Player>) p -> {
CraftingTerminalTileEntity tile = Utility.getBlockEntity(CraftingTerminalTileEntity.class, p.world, message.pos); var tile = Utility.getBlockEntity(CraftingTerminalBlockEntity.class, p.level, message.pos);
if (tile != null) if (tile != null)
tile.setGhostItems(message.stacks); tile.setGhostItems(message.stacks);
}; };
// this whole thing is a dirty hack for allowing the same packet to be used // this whole thing is a dirty hack for allowing the same packet to be used
// both client -> server and server -> client without any classloading issues // both client -> server and server -> client without any classloading issues
PlayerEntity player = ctx.get().getSender(); Player player = ctx.get().getSender();
// are we on the client? // are we on the client?
if (player == null) { if (player == null) {
ctx.get().enqueueWork(new Runnable() { ctx.get().enqueueWork(new Runnable() {

View file

@ -1,15 +1,14 @@
package de.ellpeck.prettypipes.packets; package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.math.BlockPos; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.IWorld; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.World; import net.minecraft.world.level.Level;
import net.minecraftforge.fml.network.NetworkRegistry; import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.fml.network.PacketDistributor; import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.fml.network.simple.SimpleChannel; import net.minecraftforge.network.simple.SimpleChannel;
public final class PacketHandler { public final class PacketHandler {
@ -26,12 +25,12 @@ public final class PacketHandler {
network.registerMessage(5, PacketCraftingModuleTransfer.class, PacketCraftingModuleTransfer::toBytes, PacketCraftingModuleTransfer::fromBytes, PacketCraftingModuleTransfer::onMessage); network.registerMessage(5, PacketCraftingModuleTransfer.class, PacketCraftingModuleTransfer::toBytes, PacketCraftingModuleTransfer::fromBytes, PacketCraftingModuleTransfer::onMessage);
} }
public static void sendToAllLoaded(World world, BlockPos pos, Object message) { public static void sendToAllLoaded(Level world, BlockPos pos, Object message) {
network.send(PacketDistributor.TRACKING_CHUNK.with(() -> world.getChunkAt(pos)), message); network.send(PacketDistributor.TRACKING_CHUNK.with(() -> world.getChunkAt(pos)), message);
} }
public static void sendTo(PlayerEntity player, Object message) { public static void sendTo(Player player, Object message) {
network.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) player), message); network.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), message);
} }
public static void sendToServer(Object message) { public static void sendToServer(Object message) {

View file

@ -2,12 +2,12 @@ package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.pipe.IPipeItem; import de.ellpeck.prettypipes.pipe.IPipeItem;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.PacketBuffer; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.util.math.BlockPos; import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.fml.network.NetworkEvent;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -25,16 +25,16 @@ public class PacketItemEnterPipe {
} }
public static PacketItemEnterPipe fromBytes(PacketBuffer buf) { public static PacketItemEnterPipe fromBytes(FriendlyByteBuf buf) {
PacketItemEnterPipe client = new PacketItemEnterPipe(); var client = new PacketItemEnterPipe();
client.tilePos = buf.readBlockPos(); client.tilePos = buf.readBlockPos();
client.item = buf.readCompoundTag(); client.item = buf.readNbt();
return client; return client;
} }
public static void toBytes(PacketItemEnterPipe packet, PacketBuffer buf) { public static void toBytes(PacketItemEnterPipe packet, FriendlyByteBuf buf) {
buf.writeBlockPos(packet.tilePos); buf.writeBlockPos(packet.tilePos);
buf.writeCompoundTag(packet.item); buf.writeNbt(packet.item);
} }
@SuppressWarnings("Convert2Lambda") @SuppressWarnings("Convert2Lambda")
@ -42,11 +42,11 @@ public class PacketItemEnterPipe {
ctx.get().enqueueWork(new Runnable() { ctx.get().enqueueWork(new Runnable() {
@Override @Override
public void run() { public void run() {
Minecraft mc = Minecraft.getInstance(); var mc = Minecraft.getInstance();
if (mc.world == null) if (mc.level == null)
return; return;
IPipeItem item = IPipeItem.load(message.item); var item = IPipeItem.load(message.item);
PipeTileEntity pipe = Utility.getBlockEntity(PipeTileEntity.class, mc.world, message.tilePos); var pipe = Utility.getBlockEntity(PipeBlockEntity.class, mc.level, message.tilePos);
if (pipe != null) if (pipe != null)
pipe.getItems().add(item); pipe.getItems().add(item);
} }

View file

@ -2,13 +2,9 @@ package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui; import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.inventory.container.Container;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraftforge.network.NetworkEvent;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.network.NetworkEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -30,37 +26,37 @@ public class PacketNetworkItems {
} }
public static PacketNetworkItems fromBytes(PacketBuffer buf) { public static PacketNetworkItems fromBytes(FriendlyByteBuf buf) {
PacketNetworkItems client = new PacketNetworkItems(); var client = new PacketNetworkItems();
client.items = new ArrayList<>(); client.items = new ArrayList<>();
for (int i = buf.readVarInt(); i > 0; i--) { for (var i = buf.readVarInt(); i > 0; i--) {
ItemStack stack = buf.readItemStack(); var stack = buf.readItem();
stack.setCount(buf.readVarInt()); stack.setCount(buf.readVarInt());
client.items.add(stack); client.items.add(stack);
} }
client.craftables = new ArrayList<>(); client.craftables = new ArrayList<>();
for (int i = buf.readVarInt(); i > 0; i--) for (var i = buf.readVarInt(); i > 0; i--)
client.craftables.add(buf.readItemStack()); client.craftables.add(buf.readItem());
client.currentlyCrafting = new ArrayList<>(); client.currentlyCrafting = new ArrayList<>();
for (int i = buf.readVarInt(); i > 0; i--) for (var i = buf.readVarInt(); i > 0; i--)
client.currentlyCrafting.add(buf.readItemStack()); client.currentlyCrafting.add(buf.readItem());
return client; return client;
} }
public static void toBytes(PacketNetworkItems packet, PacketBuffer buf) { public static void toBytes(PacketNetworkItems packet, FriendlyByteBuf buf) {
buf.writeVarInt(packet.items.size()); buf.writeVarInt(packet.items.size());
for (ItemStack stack : packet.items) { for (var stack : packet.items) {
ItemStack copy = stack.copy(); var copy = stack.copy();
copy.setCount(1); copy.setCount(1);
buf.writeItemStack(copy); buf.writeItem(copy);
buf.writeVarInt(stack.getCount()); buf.writeVarInt(stack.getCount());
} }
buf.writeVarInt(packet.craftables.size()); buf.writeVarInt(packet.craftables.size());
for (ItemStack stack : packet.craftables) for (var stack : packet.craftables)
buf.writeItemStack(stack); buf.writeItem(stack);
buf.writeVarInt(packet.currentlyCrafting.size()); buf.writeVarInt(packet.currentlyCrafting.size());
for (ItemStack stack : packet.currentlyCrafting) for (var stack : packet.currentlyCrafting)
buf.writeItemStack(stack); buf.writeItem(stack);
} }
@SuppressWarnings("Convert2Lambda") @SuppressWarnings("Convert2Lambda")
@ -68,9 +64,9 @@ public class PacketNetworkItems {
ctx.get().enqueueWork(new Runnable() { ctx.get().enqueueWork(new Runnable() {
@Override @Override
public void run() { public void run() {
Minecraft mc = Minecraft.getInstance(); var mc = Minecraft.getInstance();
if (mc.currentScreen instanceof ItemTerminalGui) if (mc.screen instanceof ItemTerminalGui terminal)
((ItemTerminalGui) mc.currentScreen).updateItemList(message.items, message.craftables, message.currentlyCrafting); terminal.updateItemList(message.items, message.craftables, message.currentlyCrafting);
} }
}); });
ctx.get().setPacketHandled(true); ctx.get().setPacketHandled(true);

View file

@ -1,12 +1,12 @@
package de.ellpeck.prettypipes.packets; package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.terminal.ItemTerminalTileEntity; import de.ellpeck.prettypipes.terminal.ItemTerminalBlockEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.network.PacketBuffer; import net.minecraftforge.network.NetworkEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.network.NetworkEvent;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -26,17 +26,17 @@ public class PacketRequest {
} }
public static PacketRequest fromBytes(PacketBuffer buf) { public static PacketRequest fromBytes(FriendlyByteBuf buf) {
PacketRequest packet = new PacketRequest(); var packet = new PacketRequest();
packet.pos = buf.readBlockPos(); packet.pos = buf.readBlockPos();
packet.stack = buf.readItemStack(); packet.stack = buf.readItem();
packet.amount = buf.readVarInt(); packet.amount = buf.readVarInt();
return packet; return packet;
} }
public static void toBytes(PacketRequest packet, PacketBuffer buf) { public static void toBytes(PacketRequest packet, FriendlyByteBuf buf) {
buf.writeBlockPos(packet.pos); buf.writeBlockPos(packet.pos);
buf.writeItemStack(packet.stack); buf.writeItem(packet.stack);
buf.writeVarInt(packet.amount); buf.writeVarInt(packet.amount);
} }
@ -45,8 +45,8 @@ public class PacketRequest {
ctx.get().enqueueWork(new Runnable() { ctx.get().enqueueWork(new Runnable() {
@Override @Override
public void run() { public void run() {
PlayerEntity player = ctx.get().getSender(); Player player = ctx.get().getSender();
ItemTerminalTileEntity tile = Utility.getBlockEntity(ItemTerminalTileEntity.class, player.world, message.pos); var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, player.level, message.pos);
message.stack.setCount(message.amount); message.stack.setCount(message.amount);
tile.requestItem(player, message.stack); tile.requestItem(player, message.stack);
} }

View file

@ -1,8 +1,8 @@
package de.ellpeck.prettypipes.pipe; package de.ellpeck.prettypipes.pipe;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
public interface IPipeConnectable { public interface IPipeConnectable {

View file

@ -1,16 +1,14 @@
package de.ellpeck.prettypipes.pipe; package de.ellpeck.prettypipes.pipe;
import com.google.common.collect.ImmutableMap; import com.mojang.blaze3d.vertex.PoseStack;
import com.google.common.collect.Maps;
import com.mojang.blaze3d.matrix.MatrixStack;
import de.ellpeck.prettypipes.network.NetworkEdge; import de.ellpeck.prettypipes.network.NetworkEdge;
import de.ellpeck.prettypipes.network.PipeItem; import de.ellpeck.prettypipes.network.PipeItem;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.level.Level;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.common.util.INBTSerializable;
@ -31,9 +29,9 @@ public interface IPipeItem extends INBTSerializable<CompoundTag> {
void setDestination(BlockPos startInventory, BlockPos destInventory, GraphPath<BlockPos, NetworkEdge> path); void setDestination(BlockPos startInventory, BlockPos destInventory, GraphPath<BlockPos, NetworkEdge> path);
void updateInPipe(PipeTileEntity currPipe); void updateInPipe(PipeBlockEntity currPipe);
void drop(World world, ItemStack stack); void drop(Level world, ItemStack stack);
BlockPos getDestPipe(); BlockPos getDestPipe();
@ -44,15 +42,11 @@ public interface IPipeItem extends INBTSerializable<CompoundTag> {
int getItemsOnTheWay(BlockPos goalInv); int getItemsOnTheWay(BlockPos goalInv);
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
void render(PipeTileEntity tile, MatrixStack matrixStack, Random random, float partialTicks, int light, int overlay, IRenderTypeBuffer buffer); void render(PipeBlockEntity tile, PoseStack matrixStack, Random random, float partialTicks, int light, int overlay, MultiBufferSource source);
static IPipeItem load(CompoundTag nbt) { static IPipeItem load(CompoundTag nbt) {
// TODO legacy compat, remove eventually var type = new ResourceLocation(nbt.getString("type"));
if (!nbt.contains("type")) var func = TYPES.get(type);
nbt.putString("type", PipeItem.TYPE.toString());
ResourceLocation type = new ResourceLocation(nbt.getString("type"));
BiFunction<ResourceLocation, CompoundTag, IPipeItem> func = TYPES.get(type);
return func != null ? func.apply(type, nbt) : null; return func != null ? func.apply(type, nbt) : null;
} }
} }

View file

@ -5,51 +5,36 @@ import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import net.minecraft.block.*;
import net.minecraft.block.material.Material;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.state.EnumProperty; import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.state.StateContainer; import net.minecraft.world.level.BlockGetter;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tags.FluidTags;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.shapes.IBooleanFunction;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock; import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.EnumProperty; import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.material.Material; import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.fml.network.NetworkHooks;
import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.network.NetworkHooks; import net.minecraftforge.network.NetworkHooks;
import org.apache.commons.lang3.mutable.MutableObject; import org.apache.commons.lang3.mutable.MutableObject;
@ -76,31 +61,31 @@ public class PipeBlock extends BaseEntityBlock {
.build(); .build();
static { static {
for (Direction dir : Direction.values()) for (var dir : Direction.values())
DIRECTIONS.put(dir, EnumProperty.create(dir.getName(), ConnectionType.class)); DIRECTIONS.put(dir, EnumProperty.create(dir.getName(), ConnectionType.class));
} }
public PipeBlock() { public PipeBlock() {
super(Block.Properties.of(Material.STONE).strength(2).sound(SoundType.STONE).noOcclusion()); super(Block.Properties.of(Material.STONE).strength(2).sound(SoundType.STONE).noOcclusion());
BlockState state = this.defaultBlockState().setValue(BlockStateProperties.WATERLOGGED, false); var state = this.defaultBlockState().setValue(BlockStateProperties.WATERLOGGED, false);
for (EnumProperty<ConnectionType> prop : DIRECTIONS.values()) for (var prop : DIRECTIONS.values())
state = state.setValue(prop, ConnectionType.DISCONNECTED); state = state.setValue(prop, ConnectionType.DISCONNECTED);
this.registerDefaultState(state); this.registerDefaultState(state);
} }
@Override @Override
public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult result) { public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult result) {
PipeTileEntity tile = Utility.getBlockEntity(PipeTileEntity.class, worldIn, pos); var tile = Utility.getBlockEntity(PipeBlockEntity.class, worldIn, pos);
if (tile == null) if (tile == null)
return InteractionResult.PASS; return InteractionResult.PASS;
if (!tile.canHaveModules()) if (!tile.canHaveModules())
return InteractionResult.PASS; return InteractionResult.PASS;
ItemStack stack = player.getItemInHand(handIn); var stack = player.getItemInHand(handIn);
if (stack.getItem() instanceof IModule) { if (stack.getItem() instanceof IModule) {
ItemStack copy = stack.copy(); var copy = stack.copy();
copy.setCount(1); copy.setCount(1);
ItemStack remain = ItemHandlerHelper.insertItem(tile.modules, copy, false); var remain = ItemHandlerHelper.insertItem(tile.modules, copy, false);
if (remain.isEmpty()) { if (remain.isEmpty()) {
stack.shrink(1); stack.shrink(1);
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
@ -114,62 +99,62 @@ public class PipeBlock extends BaseEntityBlock {
} }
@Override @Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(DIRECTIONS.values().toArray(new EnumProperty[0])); builder.add(DIRECTIONS.values().toArray(new EnumProperty[0]));
builder.add(BlockStateProperties.WATERLOGGED); builder.add(BlockStateProperties.WATERLOGGED);
} }
@Override @Override
public FluidState getFluidState(BlockState state) { public FluidState getFluidState(BlockState state) {
return state.get(BlockStateProperties.WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : super.getFluidState(state); return state.getValue(BlockStateProperties.WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
} }
@Override @Override
public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) { public void neighborChanged(BlockState state, Level worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) {
BlockState newState = this.createState(worldIn, pos, state); var newState = this.createState(worldIn, pos, state);
if (newState != state) { if (newState != state) {
worldIn.setBlockState(pos, newState); worldIn.setBlockAndUpdate(pos, newState);
onStateChanged(worldIn, pos, newState); onStateChanged(worldIn, pos, newState);
} }
} }
@Nullable @Nullable
@Override @Override
public BlockState getStateForPlacement(BlockItemUseContext context) { public BlockState getStateForPlacement(BlockPlaceContext context) {
return this.createState(context.getWorld(), context.getPos(), this.getDefaultState()); return this.createState(context.getLevel(), context.getClickedPos(), this.defaultBlockState());
} }
@Override @Override
public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) { public BlockState updateShape(BlockState stateIn, Direction facing, BlockState facingState, LevelAccessor worldIn, BlockPos currentPos, BlockPos facingPos) {
if (stateIn.get(BlockStateProperties.WATERLOGGED)) if (stateIn.getValue(BlockStateProperties.WATERLOGGED))
worldIn.getPendingFluidTicks().scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickRate(worldIn)); worldIn.scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickDelay(worldIn));
return super.updatePostPlacement(stateIn, facing, facingState, worldIn, currentPos, facingPos); return super.updateShape(stateIn, facing, facingState, worldIn, currentPos, facingPos);
} }
@Override @Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) { public void setPlacedBy(Level worldIn, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
onStateChanged(worldIn, pos, state); onStateChanged(worldIn, pos, state);
} }
@Override @Override
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) { public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
return this.cacheAndGetShape(state, worldIn, pos, s -> s.getShape(worldIn, pos, context), SHAPE_CACHE, null); return this.cacheAndGetShape(state, worldIn, pos, s -> s.getShape(worldIn, pos, context), SHAPE_CACHE, null);
} }
@Override @Override
public VoxelShape getCollisionShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) { public VoxelShape getCollisionShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
return this.cacheAndGetShape(state, worldIn, pos, s -> s.getCollisionShape(worldIn, pos, context), COLL_SHAPE_CACHE, s -> { return this.cacheAndGetShape(state, worldIn, pos, s -> s.getCollisionShape(worldIn, pos, context), COLL_SHAPE_CACHE, s -> {
// make the shape a bit higher so we can jump up onto a higher block // make the shape a bit higher so we can jump up onto a higher block
MutableObject<VoxelShape> newShape = new MutableObject<>(VoxelShapes.empty()); var newShape = new MutableObject<VoxelShape>(Shapes.empty());
s.forEachBox((x1, y1, z1, x2, y2, z2) -> newShape.setValue(VoxelShapes.combine(VoxelShapes.create(x1, y1, z1, x2, y2 + 3 / 16F, z2), newShape.getValue(), IBooleanFunction.OR))); s.forAllBoxes((x1, y1, z1, x2, y2, z2) -> newShape.setValue(Shapes.join(Shapes.create(x1, y1, z1, x2, y2 + 3 / 16F, z2), newShape.getValue(), BooleanOp.OR)));
return newShape.getValue().simplify(); return newShape.getValue().optimize();
}); });
} }
private VoxelShape cacheAndGetShape(BlockState state, IBlockReader worldIn, BlockPos pos, Function<BlockState, VoxelShape> coverShapeSelector, Map<Pair<BlockState, BlockState>, VoxelShape> cache, Function<VoxelShape, VoxelShape> shapeModifier) { private VoxelShape cacheAndGetShape(BlockState state, BlockGetter worldIn, BlockPos pos, Function<BlockState, VoxelShape> coverShapeSelector, Map<Pair<BlockState, BlockState>, VoxelShape> cache, Function<VoxelShape, VoxelShape> shapeModifier) {
VoxelShape coverShape = null; VoxelShape coverShape = null;
BlockState cover = null; BlockState cover = null;
PipeTileEntity tile = Utility.getBlockEntity(PipeTileEntity.class, worldIn, pos); var tile = Utility.getBlockEntity(PipeBlockEntity.class, worldIn, pos);
if (tile != null && tile.cover != null) { if (tile != null && tile.cover != null) {
cover = tile.cover; cover = tile.cover;
// try catch since the block might expect to find itself at the position // try catch since the block might expect to find itself at the position
@ -178,88 +163,88 @@ public class PipeBlock extends BaseEntityBlock {
} catch (Exception ignored) { } catch (Exception ignored) {
} }
} }
Pair<BlockState, BlockState> key = Pair.of(state, cover); var key = Pair.of(state, cover);
VoxelShape shape = cache.get(key); var shape = cache.get(key);
if (shape == null) { if (shape == null) {
shape = CENTER_SHAPE; shape = CENTER_SHAPE;
for (Map.Entry<Direction, EnumProperty<ConnectionType>> entry : DIRECTIONS.entrySet()) { for (var entry : DIRECTIONS.entrySet()) {
if (state.get(entry.getValue()).isConnected()) if (state.getValue(entry.getValue()).isConnected())
shape = VoxelShapes.or(shape, DIR_SHAPES.get(entry.getKey())); shape = Shapes.or(shape, DIR_SHAPES.get(entry.getKey()));
} }
if (shapeModifier != null) if (shapeModifier != null)
shape = shapeModifier.apply(shape); shape = shapeModifier.apply(shape);
if (coverShape != null) if (coverShape != null)
shape = VoxelShapes.or(shape, coverShape); shape = Shapes.or(shape, coverShape);
cache.put(key, shape); cache.put(key, shape);
} }
return shape; return shape;
} }
private BlockState createState(World world, BlockPos pos, BlockState curr) { private BlockState createState(Level world, BlockPos pos, BlockState curr) {
BlockState state = this.getDefaultState(); var state = this.defaultBlockState();
FluidState fluid = world.getFluidState(pos); var fluid = world.getFluidState(pos);
if (fluid.isTagged(FluidTags.WATER) && fluid.getLevel() == 8) if (fluid.is(FluidTags.WATER) && fluid.getAmount() == 8)
state = state.with(BlockStateProperties.WATERLOGGED, true); state = state.setValue(BlockStateProperties.WATERLOGGED, true);
for (Direction dir : Direction.values()) { for (var dir : Direction.values()) {
EnumProperty<ConnectionType> prop = DIRECTIONS.get(dir); var prop = DIRECTIONS.get(dir);
ConnectionType type = this.getConnectionType(world, pos, dir, state); var type = this.getConnectionType(world, pos, dir, state);
// don't reconnect on blocked faces // don't reconnect on blocked faces
if (type.isConnected() && curr.get(prop) == ConnectionType.BLOCKED) if (type.isConnected() && curr.getValue(prop) == ConnectionType.BLOCKED)
type = ConnectionType.BLOCKED; type = ConnectionType.BLOCKED;
state = state.with(prop, type); state = state.setValue(prop, type);
} }
return state; return state;
} }
protected ConnectionType getConnectionType(World world, BlockPos pos, Direction direction, BlockState state) { protected ConnectionType getConnectionType(Level world, BlockPos pos, Direction direction, BlockState state) {
BlockPos offset = pos.offset(direction); var offset = pos.relative(direction);
if (!world.isBlockLoaded(offset)) if (!world.isLoaded(offset))
return ConnectionType.DISCONNECTED; return ConnectionType.DISCONNECTED;
Direction opposite = direction.getOpposite(); var opposite = direction.getOpposite();
TileEntity tile = world.getTileEntity(offset); var tile = world.getBlockEntity(offset);
if (tile != null) { if (tile != null) {
IPipeConnectable connectable = tile.getCapability(Registry.pipeConnectableCapability, opposite).orElse(null); var connectable = tile.getCapability(Registry.pipeConnectableCapability, opposite).orElse(null);
if (connectable != null) if (connectable != null)
return connectable.getConnectionType(pos, direction); return connectable.getConnectionType(pos, direction);
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, opposite).orElse(null); var handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, opposite).orElse(null);
if (handler != null) if (handler != null)
return ConnectionType.CONNECTED; return ConnectionType.CONNECTED;
} }
IItemHandler blockHandler = Utility.getBlockItemHandler(world, offset, opposite); var blockHandler = Utility.getBlockItemHandler(world, offset, opposite);
if (blockHandler != null) if (blockHandler != null)
return ConnectionType.CONNECTED; return ConnectionType.CONNECTED;
BlockState offState = world.getBlockState(offset); var offState = world.getBlockState(offset);
if (hasLegsTo(world, offState, offset, direction)) { if (hasLegsTo(world, offState, offset, direction)) {
if (DIRECTIONS.values().stream().noneMatch(d -> state.get(d) == ConnectionType.LEGS)) if (DIRECTIONS.values().stream().noneMatch(d -> state.getValue(d) == ConnectionType.LEGS))
return ConnectionType.LEGS; return ConnectionType.LEGS;
} }
return ConnectionType.DISCONNECTED; return ConnectionType.DISCONNECTED;
} }
protected static boolean hasLegsTo(World world, BlockState state, BlockPos pos, Direction direction) { protected static boolean hasLegsTo(Level world, BlockState state, BlockPos pos, Direction direction) {
if (state.getBlock() instanceof WallBlock || state.getBlock() instanceof FenceBlock) if (state.getBlock() instanceof WallBlock || state.getBlock() instanceof FenceBlock)
return direction == Direction.DOWN; return direction == Direction.DOWN;
if (state.getMaterial() == Material.ROCK || state.getMaterial() == Material.IRON) if (state.getMaterial() == Material.STONE || state.getMaterial() == Material.METAL)
return hasEnoughSolidSide(world, pos, direction.getOpposite()); return canSupportCenter(world, pos, direction.getOpposite());
return false; return false;
} }
public static void onStateChanged(World world, BlockPos pos, BlockState newState) { public static void onStateChanged(Level world, BlockPos pos, BlockState newState) {
// wait a few ticks before checking if we have to drop our modules, so that things like iron -> gold chest work // wait a few ticks before checking if we have to drop our modules, so that things like iron -> gold chest work
PipeTileEntity tile = Utility.getBlockEntity(PipeTileEntity.class, world, pos); var tile = Utility.getBlockEntity(PipeBlockEntity.class, world, pos);
if (tile != null) if (tile != null)
tile.moduleDropCheck = 5; tile.moduleDropCheck = 5;
PipeNetwork network = PipeNetwork.get(world); var network = PipeNetwork.get(world);
int connections = 0; var connections = 0;
boolean force = false; var force = false;
for (Direction dir : Direction.values()) { for (var dir : Direction.values()) {
ConnectionType value = newState.get(DIRECTIONS.get(dir)); var value = newState.getValue(DIRECTIONS.get(dir));
if (!value.isConnected()) if (!value.isConnected())
continue; continue;
connections++; connections++;
BlockState otherState = world.getBlockState(pos.offset(dir)); var otherState = world.getBlockState(pos.relative(dir));
// force a node if we're connecting to a different block (inventory etc.) // force a node if we're connecting to a different block (inventory etc.)
if (otherState.getBlock() != newState.getBlock()) { if (otherState.getBlock() != newState.getBlock()) {
force = true; force = true;
@ -275,53 +260,59 @@ public class PipeBlock extends BaseEntityBlock {
} }
@Override @Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) { public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
if (state.getBlock() != newState.getBlock()) { if (state.getBlock() != newState.getBlock()) {
PipeNetwork network = PipeNetwork.get(worldIn); var network = PipeNetwork.get(worldIn);
network.removeNode(pos); network.removeNode(pos);
network.onPipeChanged(pos, state); network.onPipeChanged(pos, state);
super.onReplaced(state, worldIn, pos, newState, isMoving); super.onRemove(state, worldIn, pos, newState, isMoving);
} }
} }
@Override @Override
public void onBlockHarvested(World worldIn, BlockPos pos, BlockState state, PlayerEntity player) { public void playerWillDestroy(Level worldIn, BlockPos pos, BlockState state, Player player) {
dropItems(worldIn, pos, player); dropItems(worldIn, pos, player);
super.onBlockHarvested(worldIn, pos, state, player); super.playerWillDestroy(worldIn, pos, state, player);
} }
@Override @Override
public boolean hasComparatorInputOverride(BlockState state) { public boolean hasAnalogOutputSignal(BlockState state) {
return true; return true;
} }
@Override @Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) { public int getAnalogOutputSignal(BlockState state, Level world, BlockPos pos) {
PipeTileEntity pipe = Utility.getBlockEntity(PipeTileEntity.class, worldIn, pos); var pipe = Utility.getBlockEntity(PipeBlockEntity.class, world, pos);
if (pipe == null) if (pipe == null)
return 0; return 0;
return Math.min(15, pipe.getItems().size()); return Math.min(15, pipe.getItems().size());
} }
@Nullable @org.jetbrains.annotations.Nullable
@Override @Override
public TileEntity createNewTileEntity(IBlockReader worldIn) { public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new PipeTileEntity(); return new PipeBlockEntity(pos, state);
} }
@Override @Override
public BlockRenderType getRenderType(BlockState state) { public RenderShape getRenderShape(BlockState state) {
return BlockRenderType.MODEL; return RenderShape.MODEL;
} }
public static void dropItems(World worldIn, BlockPos pos, PlayerEntity player) { @org.jetbrains.annotations.Nullable
PipeTileEntity tile = Utility.getBlockEntity(PipeTileEntity.class, worldIn, pos); @Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
return createTickerHelper(type, Registry.pipeBlockEntity, PipeBlockEntity::tick);
}
public static void dropItems(Level worldIn, BlockPos pos, Player player) {
var tile = Utility.getBlockEntity(PipeBlockEntity.class, worldIn, pos);
if (tile != null) { if (tile != null) {
Utility.dropInventory(tile, tile.modules); Utility.dropInventory(tile, tile.modules);
for (IPipeItem item : tile.getItems()) for (var item : tile.getItems())
item.drop(worldIn, item.getContent()); item.drop(worldIn, item.getContent());
if (tile.cover != null) if (tile.cover != null)
tile.removeCover(player, Hand.MAIN_HAND); tile.removeCover(player, InteractionHand.MAIN_HAND);
} }
} }
} }

View file

@ -10,38 +10,32 @@ import de.ellpeck.prettypipes.network.NetworkLock;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.containers.MainPipeContainer; import de.ellpeck.prettypipes.pipe.containers.MainPipeContainer;
import de.ellpeck.prettypipes.pressurizer.PressurizerBlockEntity; import de.ellpeck.prettypipes.pressurizer.PressurizerBlockEntity;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.Direction;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.Item;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NBTUtil; import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.NetworkManager; import net.minecraft.nbt.Tag;
import net.minecraft.network.play.server.SUpdateTileEntityPacket; import net.minecraft.network.Connection;
import net.minecraft.profiler.IProfiler; import net.minecraft.network.chat.Component;
import net.minecraft.tileentity.ITickableTileEntity; import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.tileentity.TileEntity; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.tileentity.TileEntityType; import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Direction; import net.minecraft.world.Containers;
import net.minecraft.util.Hand; import net.minecraft.world.InteractionHand;
import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.world.MenuProvider;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Player;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.server.ServerWorld; import net.minecraft.world.phys.AABB;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.common.util.Lazy; import net.minecraftforge.common.util.Lazy;
import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.CapabilityItemHandler;
@ -57,7 +51,7 @@ import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
public class PipeTileEntity extends BlockEntity implements INamedContainerProvider, ITickableTileEntity, IPipeConnectable { public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeConnectable {
public final ItemStackHandler modules = new ItemStackHandler(3) { public final ItemStackHandler modules = new ItemStackHandler(3) {
@Override @Override
@ -65,7 +59,7 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
var item = stack.getItem(); var item = stack.getItem();
if (!(item instanceof IModule module)) if (!(item instanceof IModule module))
return false; return false;
return PipeTileEntity.this.streamModules().allMatch(m -> module.isCompatible(stack, PipeTileEntity.this, m.getRight()) && m.getRight().isCompatible(m.getLeft(), PipeTileEntity.this, module)); return PipeBlockEntity.this.streamModules().allMatch(m -> module.isCompatible(stack, PipeBlockEntity.this, m.getRight()) && m.getRight().isCompatible(m.getLeft(), PipeBlockEntity.this, module));
} }
@Override @Override
@ -81,9 +75,13 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
protected List<IPipeItem> items; protected List<IPipeItem> items;
private int lastItemAmount; private int lastItemAmount;
private int priority; private int priority;
private final LazyOptional<PipeTileEntity> lazyThis = LazyOptional.of(() -> this); private final LazyOptional<PipeBlockEntity> lazyThis = LazyOptional.of(() -> this);
private final Lazy<Integer> workRandomizer = Lazy.of(() -> this.level.random.nextInt(200)); private final Lazy<Integer> workRandomizer = Lazy.of(() -> this.level.random.nextInt(200));
public PipeBlockEntity(BlockPos pos, BlockState state) {
super(Registry.pipeBlockEntity, pos, state);
}
@Override @Override
public void onChunkUnloaded() { public void onChunkUnloaded() {
PipeNetwork.get(this.level).uncachePipe(this.worldPosition); PipeNetwork.get(this.level).uncachePipe(this.worldPosition);
@ -96,9 +94,9 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
compound.put("requests", Utility.serializeAll(this.craftIngredientRequests)); compound.put("requests", Utility.serializeAll(this.craftIngredientRequests));
if (this.cover != null) if (this.cover != null)
compound.put("cover", NbtUtils.writeBlockState(this.cover)); compound.put("cover", NbtUtils.writeBlockState(this.cover));
ListTag results = new ListTag(); var results = new ListTag();
for (Pair<BlockPos, ItemStack> triple : this.craftResultRequests) { for (var triple : this.craftResultRequests) {
CompoundTag nbt = new CompoundTag(); var nbt = new CompoundTag();
nbt.putLong("dest_pipe", triple.getLeft().asLong()); nbt.putLong("dest_pipe", triple.getLeft().asLong());
nbt.put("item", triple.getRight().serializeNBT()); nbt.put("item", triple.getRight().serializeNBT());
results.add(nbt); results.add(nbt);
@ -113,11 +111,11 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
this.moduleDropCheck = compound.getInt("module_drop_check"); this.moduleDropCheck = compound.getInt("module_drop_check");
this.cover = compound.contains("cover") ? NbtUtils.readBlockState(compound.getCompound("cover")) : null; this.cover = compound.contains("cover") ? NbtUtils.readBlockState(compound.getCompound("cover")) : null;
this.craftIngredientRequests.clear(); this.craftIngredientRequests.clear();
this.craftIngredientRequests.addAll(Utility.deserializeAll(compound.getList("requests", NBT.TAG_COMPOUND), NetworkLock::new)); this.craftIngredientRequests.addAll(Utility.deserializeAll(compound.getList("requests", Tag.TAG_COMPOUND), NetworkLock::new));
this.craftResultRequests.clear(); this.craftResultRequests.clear();
ListTag results = compound.getList("craft_results", NBT.TAG_COMPOUND); var results = compound.getList("craft_results", Tag.TAG_COMPOUND);
for (int i = 0; i < results.size(); i++) { for (var i = 0; i < results.size(); i++) {
CompoundTag nbt = results.getCompound(i); var nbt = results.getCompound(i);
this.craftResultRequests.add(Pair.of( this.craftResultRequests.add(Pair.of(
BlockPos.of(nbt.getLong("dest_pipe")), BlockPos.of(nbt.getLong("dest_pipe")),
ItemStack.of(nbt.getCompound("item")))); ItemStack.of(nbt.getCompound("item"))));
@ -128,72 +126,27 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
@Override @Override
public CompoundTag getUpdateTag() { public CompoundTag getUpdateTag() {
// sync pipe items on load // sync pipe items on load
CompoundTag nbt = this.write(new CompoundTag()); var nbt = this.save(new CompoundTag());
nbt.put("items", Utility.serializeAll(this.getItems())); nbt.put("items", Utility.serializeAll(this.getItems()));
return nbt; return nbt;
} }
@Override @Override
public void handleUpdateTag(BlockState state, CompoundTag nbt) { public void handleUpdateTag(CompoundTag nbt) {
this.read(state, nbt); this.load(nbt);
List<IPipeItem> items = this.getItems(); var items = this.getItems();
items.clear(); items.clear();
items.addAll(Utility.deserializeAll(nbt.getList("items", NBT.TAG_COMPOUND), IPipeItem::load)); items.addAll(Utility.deserializeAll(nbt.getList("items", Tag.TAG_COMPOUND), IPipeItem::load));
} }
@Override @Override
public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) { public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) {
this.read(this.getBlockState(), pkt.getNbtCompound()); this.load(pkt.getTag());
}
@Override
public void tick() {
// invalidate our pressurizer reference if it was removed
if (this.pressurizer != null && this.pressurizer.isRemoved())
this.pressurizer = null;
if (!this.world.isAreaLoaded(this.pos, 1))
return;
IProfiler profiler = this.world.getProfiler();
if (!this.world.isRemote) {
// drop modules here to give a bit of time for blocks to update (iron -> gold chest etc.)
if (this.moduleDropCheck > 0) {
this.moduleDropCheck--;
if (this.moduleDropCheck <= 0 && !this.canHaveModules())
Utility.dropInventory(this, this.modules);
}
profiler.startSection("ticking_modules");
int prio = 0;
Iterator<Pair<ItemStack, IModule>> modules = this.streamModules().iterator();
while (modules.hasNext()) {
Pair<ItemStack, IModule> module = modules.next();
module.getRight().tick(module.getLeft(), this);
prio += module.getRight().getPriority(module.getLeft(), this);
}
if (prio != this.priority) {
this.priority = prio;
// clear the cache so that it's reevaluated based on priority
PipeNetwork.get(this.world).clearDestinationCache(this.pos);
}
profiler.endSection();
}
profiler.startSection("ticking_items");
List<IPipeItem> items = this.getItems();
for (int i = items.size() - 1; i >= 0; i--)
items.get(i).updateInPipe(this);
if (items.size() != this.lastItemAmount) {
this.lastItemAmount = items.size();
this.world.updateComparatorOutputLevel(this.pos, this.getBlockState().getBlock());
}
profiler.endSection();
} }
public List<IPipeItem> getItems() { public List<IPipeItem> getItems() {
if (this.items == null) if (this.items == null)
this.items = PipeNetwork.get(this.world).getItemsInPipe(this.pos); this.items = PipeNetwork.get(this.level).getItemsInPipe(this.worldPosition);
return this.items; return this.items;
} }
@ -206,23 +159,23 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
} }
public boolean isConnected(Direction dir) { public boolean isConnected(Direction dir) {
return this.getBlockState().get(PipeBlock.DIRECTIONS.get(dir)).isConnected(); return this.getBlockState().getValue(PipeBlock.DIRECTIONS.get(dir)).isConnected();
} }
public Pair<BlockPos, ItemStack> getAvailableDestinationOrConnectable(ItemStack stack, boolean force, boolean preventOversending) { public Pair<BlockPos, ItemStack> getAvailableDestinationOrConnectable(ItemStack stack, boolean force, boolean preventOversending) {
Pair<BlockPos, ItemStack> dest = this.getAvailableDestination(stack, force, preventOversending); var dest = this.getAvailableDestination(stack, force, preventOversending);
if (dest != null) if (dest != null)
return dest; return dest;
// if there's no available destination, try inserting into terminals etc. // if there's no available destination, try inserting into terminals etc.
for (Direction dir : Direction.values()) { for (var dir : Direction.values()) {
IPipeConnectable connectable = this.getPipeConnectable(dir); var connectable = this.getPipeConnectable(dir);
if (connectable == null) if (connectable == null)
continue; continue;
ItemStack connectableRemain = connectable.insertItem(this.getPos(), dir, stack, true); var connectableRemain = connectable.insertItem(this.worldPosition, dir, stack, true);
if (connectableRemain.getCount() != stack.getCount()) { if (connectableRemain.getCount() != stack.getCount()) {
ItemStack inserted = stack.copy(); var inserted = stack.copy();
inserted.shrink(connectableRemain.getCount()); inserted.shrink(connectableRemain.getCount());
return Pair.of(this.getPos().offset(dir), inserted); return Pair.of(this.worldPosition.relative(dir), inserted);
} }
} }
return null; return null;
@ -233,47 +186,47 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
return null; return null;
if (!force && this.streamModules().anyMatch(m -> !m.getRight().canAcceptItem(m.getLeft(), this, stack))) if (!force && this.streamModules().anyMatch(m -> !m.getRight().canAcceptItem(m.getLeft(), this, stack)))
return null; return null;
for (Direction dir : Direction.values()) { for (var dir : Direction.values()) {
IItemHandler handler = this.getItemHandler(dir); var handler = this.getItemHandler(dir);
if (handler == null) if (handler == null)
continue; continue;
ItemStack remain = ItemHandlerHelper.insertItem(handler, stack, true); var remain = ItemHandlerHelper.insertItem(handler, stack, true);
// did we insert anything? // did we insert anything?
if (remain.getCount() == stack.getCount()) if (remain.getCount() == stack.getCount())
continue; continue;
ItemStack toInsert = stack.copy(); var toInsert = stack.copy();
toInsert.shrink(remain.getCount()); toInsert.shrink(remain.getCount());
// limit to the max amount that modules allow us to insert // limit to the max amount that modules allow us to insert
int maxAmount = this.streamModules().mapToInt(m -> m.getRight().getMaxInsertionAmount(m.getLeft(), this, stack, handler)).min().orElse(Integer.MAX_VALUE); var maxAmount = this.streamModules().mapToInt(m -> m.getRight().getMaxInsertionAmount(m.getLeft(), this, stack, handler)).min().orElse(Integer.MAX_VALUE);
if (maxAmount < toInsert.getCount()) if (maxAmount < toInsert.getCount())
toInsert.setCount(maxAmount); toInsert.setCount(maxAmount);
BlockPos offset = this.pos.offset(dir); var offset = this.worldPosition.relative(dir);
if (preventOversending || maxAmount < Integer.MAX_VALUE) { if (preventOversending || maxAmount < Integer.MAX_VALUE) {
PipeNetwork network = PipeNetwork.get(this.world); var network = PipeNetwork.get(this.level);
// these are the items that are currently in the pipes, going to this inventory // these are the items that are currently in the pipes, going to this inventory
int onTheWay = network.getItemsOnTheWay(offset, null); var onTheWay = network.getItemsOnTheWay(offset, null);
if (onTheWay > 0) { if (onTheWay > 0) {
if (maxAmount < Integer.MAX_VALUE) { if (maxAmount < Integer.MAX_VALUE) {
// these are the items on the way, limited to items of the same type as stack // these are the items on the way, limited to items of the same type as stack
int onTheWaySame = network.getItemsOnTheWay(offset, stack); var onTheWaySame = network.getItemsOnTheWay(offset, stack);
// check if any modules are limiting us // check if any modules are limiting us
if (toInsert.getCount() + onTheWaySame > maxAmount) if (toInsert.getCount() + onTheWaySame > maxAmount)
toInsert.setCount(maxAmount - onTheWaySame); toInsert.setCount(maxAmount - onTheWaySame);
} }
// totalSpace will be the amount of items that fit into the attached container // totalSpace will be the amount of items that fit into the attached container
int totalSpace = 0; var totalSpace = 0;
for (int i = 0; i < handler.getSlots(); i++) { for (var i = 0; i < handler.getSlots(); i++) {
ItemStack copy = stack.copy(); var copy = stack.copy();
int maxStackSize = copy.getMaxStackSize(); var maxStackSize = copy.getMaxStackSize();
// if the container can store more than 64 items in this slot, then it's likely // if the container can store more than 64 items in this slot, then it's likely
// a barrel or similar, meaning that the slot limit matters more than the max stack size // a barrel or similar, meaning that the slot limit matters more than the max stack size
int limit = handler.getSlotLimit(i); var limit = handler.getSlotLimit(i);
if (limit > 64) if (limit > 64)
maxStackSize = limit; maxStackSize = limit;
copy.setCount(maxStackSize); copy.setCount(maxStackSize);
// this is an inaccurate check since it ignores the fact that some slots might // this is an inaccurate check since it ignores the fact that some slots might
// have space for items of other types, but it'll be good enough for us // have space for items of other types, but it'll be good enough for us
ItemStack left = handler.insertItem(i, copy, true); var left = handler.insertItem(i, copy, true);
totalSpace += maxStackSize - left.getCount(); totalSpace += maxStackSize - left.getCount();
} }
// if the items on the way plus the items we're trying to move are too much, reduce // if the items on the way plus the items we're trying to move are too much, reduce
@ -293,8 +246,8 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
} }
public float getItemSpeed(ItemStack stack) { public float getItemSpeed(ItemStack stack) {
float moduleSpeed = (float) this.streamModules().mapToDouble(m -> m.getRight().getItemSpeedIncrease(m.getLeft(), this)).sum(); var moduleSpeed = (float) this.streamModules().mapToDouble(m -> m.getRight().getItemSpeedIncrease(m.getLeft(), this)).sum();
float pressureSpeed = this.pressurizer != null && this.pressurizer.pressurizeItem(stack, true) ? 0.45F : 0; var pressureSpeed = this.pressurizer != null && this.pressurizer.pressurizeItem(stack, true) ? 0.45F : 0;
return 0.05F + moduleSpeed + pressureSpeed; return 0.05F + moduleSpeed + pressureSpeed;
} }
@ -309,13 +262,13 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
} }
public int getCraftableAmount(Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) { public int getCraftableAmount(Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
int total = 0; var total = 0;
Iterator<Pair<ItemStack, IModule>> modules = this.streamModules().iterator(); var modules = this.streamModules().iterator();
while (modules.hasNext()) { while (modules.hasNext()) {
Pair<ItemStack, IModule> module = modules.next(); var module = modules.next();
// make sure we don't factor in recursive dependencies like ingot -> block -> ingot etc. // make sure we don't factor in recursive dependencies like ingot -> block -> ingot etc.
if (dependencyChain.stream().noneMatch(d -> ItemEquality.compareItems(module.getLeft(), d, ItemEquality.NBT))) { if (dependencyChain.stream().noneMatch(d -> ItemEquality.compareItems(module.getLeft(), d, ItemEquality.NBT))) {
int amount = module.getRight().getCraftableAmount(module.getLeft(), this, unavailableConsumer, stack, dependencyChain); var amount = module.getRight().getCraftableAmount(module.getLeft(), this, unavailableConsumer, stack, dependencyChain);
if (amount > 0) if (amount > 0)
total += amount; total += amount;
} }
@ -324,9 +277,9 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
} }
public ItemStack craft(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) { public ItemStack craft(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
Iterator<Pair<ItemStack, IModule>> modules = this.streamModules().iterator(); var modules = this.streamModules().iterator();
while (modules.hasNext()) { while (modules.hasNext()) {
Pair<ItemStack, IModule> module = modules.next(); var module = modules.next();
stack = module.getRight().craft(module.getLeft(), this, destPipe, unavailableConsumer, stack, dependencyChain); stack = module.getRight().craft(module.getLeft(), this, destPipe, unavailableConsumer, stack, dependencyChain);
if (stack.isEmpty()) if (stack.isEmpty())
break; break;
@ -335,24 +288,24 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
} }
public IItemHandler getItemHandler(Direction dir) { public IItemHandler getItemHandler(Direction dir) {
IItemHandler handler = this.getNeighborCap(dir, CapabilityItemHandler.ITEM_HANDLER_CAPABILITY); var handler = this.getNeighborCap(dir, CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
if (handler != null) if (handler != null)
return handler; return handler;
return Utility.getBlockItemHandler(this.world, this.pos.offset(dir), dir.getOpposite()); return Utility.getBlockItemHandler(this.level, this.worldPosition.relative(dir), dir.getOpposite());
} }
public <T> T getNeighborCap(Direction dir, Capability<T> cap) { public <T> T getNeighborCap(Direction dir, Capability<T> cap) {
if (!this.isConnected(dir)) if (!this.isConnected(dir))
return null; return null;
BlockPos pos = this.pos.offset(dir); var pos = this.worldPosition.relative(dir);
TileEntity tile = this.world.getTileEntity(pos); var tile = this.level.getBlockEntity(pos);
if (tile != null) if (tile != null)
return tile.getCapability(cap, dir.getOpposite()).orElse(null); return tile.getCapability(cap, dir.getOpposite()).orElse(null);
return null; return null;
} }
public IPipeConnectable getPipeConnectable(Direction dir) { public IPipeConnectable getPipeConnectable(Direction dir) {
TileEntity tile = this.world.getTileEntity(this.pos.offset(dir)); var tile = this.level.getBlockEntity(this.worldPosition.relative(dir));
if (tile != null) if (tile != null)
return tile.getCapability(Registry.pipeConnectableCapability, dir.getOpposite()).orElse(null); return tile.getCapability(Registry.pipeConnectableCapability, dir.getOpposite()).orElse(null);
return null; return null;
@ -363,11 +316,11 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
} }
public boolean canHaveModules() { public boolean canHaveModules() {
for (Direction dir : Direction.values()) { for (var dir : Direction.values()) {
if (this.isConnectedInventory(dir)) if (this.isConnectedInventory(dir))
return true; return true;
IPipeConnectable connectable = this.getPipeConnectable(dir); var connectable = this.getPipeConnectable(dir);
if (connectable != null && connectable.allowsModules(this.pos, dir)) if (connectable != null && connectable.allowsModules(this.worldPosition, dir))
return true; return true;
} }
return false; return false;
@ -379,8 +332,8 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
public Stream<Pair<ItemStack, IModule>> streamModules() { public Stream<Pair<ItemStack, IModule>> streamModules() {
Stream.Builder<Pair<ItemStack, IModule>> builder = Stream.builder(); Stream.Builder<Pair<ItemStack, IModule>> builder = Stream.builder();
for (int i = 0; i < this.modules.getSlots(); i++) { for (var i = 0; i < this.modules.getSlots(); i++) {
ItemStack stack = this.modules.getStackInSlot(i); var stack = this.modules.getStackInSlot(i);
if (stack.isEmpty()) if (stack.isEmpty())
continue; continue;
builder.accept(Pair.of(stack, (IModule) stack.getItem())); builder.accept(Pair.of(stack, (IModule) stack.getItem()));
@ -388,17 +341,17 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
return builder.build(); return builder.build();
} }
public void removeCover(PlayerEntity player, Hand hand) { public void removeCover(Player player, InteractionHand hand) {
if (this.world.isRemote) if (this.level.isClientSide)
return; return;
List<ItemStack> drops = Block.getDrops(this.cover, (ServerWorld) this.world, this.pos, null, player, player.getHeldItem(hand)); var drops = Block.getDrops(this.cover, (ServerLevel) this.level, this.worldPosition, null, player, player.getItemInHand(hand));
for (ItemStack drop : drops) for (var drop : drops)
Block.spawnAsEntity(this.world, this.pos, drop); Containers.dropItemStack(this.level, this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ(), drop);
this.cover = null; this.cover = null;
} }
public boolean shouldWorkNow(int speed) { public boolean shouldWorkNow(int speed) {
return (this.world.getGameTime() + this.workRandomizer.get()) % speed == 0; return (this.level.getGameTime() + this.workRandomizer.get()) % speed == 0;
} }
public int getNextNode(List<BlockPos> nodes, int index) { public int getNextNode(List<BlockPos> nodes, int index) {
@ -414,31 +367,31 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
} }
@Override @Override
public void remove() { public void setRemoved() {
super.remove(); super.setRemoved();
this.getItems().clear(); this.getItems().clear();
PipeNetwork network = PipeNetwork.get(this.world); var network = PipeNetwork.get(this.level);
for (NetworkLock lock : this.craftIngredientRequests) for (var lock : this.craftIngredientRequests)
network.resolveNetworkLock(lock); network.resolveNetworkLock(lock);
this.lazyThis.invalidate(); this.lazyThis.invalidate();
} }
@Override @Override
public ITextComponent getDisplayName() { public Component getDisplayName() {
return new TranslationTextComponent("container." + PrettyPipes.ID + ".pipe"); return new TranslatableComponent("container." + PrettyPipes.ID + ".pipe");
} }
@Nullable @Nullable
@Override @Override
public Container createMenu(int window, PlayerInventory inv, PlayerEntity player) { public AbstractContainerMenu createMenu(int window, Inventory inv, Player player) {
return new MainPipeContainer(Registry.pipeContainer, window, player, PipeTileEntity.this.pos); return new MainPipeContainer(Registry.pipeContainer, window, player, PipeBlockEntity.this.worldPosition);
} }
@Override @Override
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public AxisAlignedBB getRenderBoundingBox() { public AABB getRenderBoundingBox() {
// our render bounding box should always be the full block in case we're covered // our render bounding box should always be the full block in case we're covered
return new AxisAlignedBB(this.pos); return new AABB(this.worldPosition);
} }
@Override @Override
@ -450,9 +403,54 @@ public class PipeTileEntity extends BlockEntity implements INamedContainerProvid
@Override @Override
public ConnectionType getConnectionType(BlockPos pipePos, Direction direction) { public ConnectionType getConnectionType(BlockPos pipePos, Direction direction) {
BlockState state = this.world.getBlockState(pipePos.offset(direction)); var state = this.level.getBlockState(pipePos.relative(direction));
if (state.get(PipeBlock.DIRECTIONS.get(direction.getOpposite())) == ConnectionType.BLOCKED) if (state.getValue(PipeBlock.DIRECTIONS.get(direction.getOpposite())) == ConnectionType.BLOCKED)
return ConnectionType.BLOCKED; return ConnectionType.BLOCKED;
return ConnectionType.CONNECTED; return ConnectionType.CONNECTED;
} }
public static void tick(Level level, BlockPos pos, BlockState state, PipeBlockEntity pipe) {
// invalidate our pressurizer reference if it was removed
if (pipe.pressurizer != null && pipe.pressurizer.isRemoved())
pipe.pressurizer = null;
if (!pipe.level.isAreaLoaded(pipe.worldPosition, 1))
return;
var profiler = pipe.level.getProfiler();
if (!pipe.level.isClientSide) {
// drop modules here to give a bit of time for blocks to update (iron -> gold chest etc.)
if (pipe.moduleDropCheck > 0) {
pipe.moduleDropCheck--;
if (pipe.moduleDropCheck <= 0 && !pipe.canHaveModules())
Utility.dropInventory(pipe, pipe.modules);
}
profiler.push("ticking_modules");
var prio = 0;
var modules = pipe.streamModules().iterator();
while (modules.hasNext()) {
var module = modules.next();
module.getRight().tick(module.getLeft(), pipe);
prio += module.getRight().getPriority(module.getLeft(), pipe);
}
if (prio != pipe.priority) {
pipe.priority = prio;
// clear the cache so that it's reevaluated based on priority
PipeNetwork.get(pipe.level).clearDestinationCache(pipe.worldPosition);
}
profiler.pop();
}
profiler.push("ticking_items");
var items = pipe.getItems();
for (var i = items.size() - 1; i >= 0; i--)
items.get(i).updateInPipe(pipe);
if (items.size() != pipe.lastItemAmount) {
pipe.lastItemAmount = items.size();
pipe.level.updateNeighbourForOutputSignal(pipe.worldPosition, pipe.getBlockState().getBlock());
}
profiler.pop();
}
} }

View file

@ -1,54 +1,51 @@
package de.ellpeck.prettypipes.pipe; package de.ellpeck.prettypipes.pipe;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BlockModelRenderer; import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup; import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer; import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.client.ForgeHooksClient; import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.model.data.EmptyModelData; import net.minecraftforge.client.model.pipeline.ForgeBlockModelRenderer;
import java.util.Random; import java.util.Random;
public class PipeRenderer extends TileEntityRenderer<PipeTileEntity> { public class PipeRenderer implements BlockEntityRenderer<PipeBlockEntity> {
private final Random random = new Random(); private final Random random = new Random();
public PipeRenderer(TileEntityRendererDispatcher disp) { public PipeRenderer(BlockEntityRendererProvider.Context ctx) {
super(disp);
} }
@Override @Override
public void render(PipeTileEntity tile, float partialTicks, MatrixStack matrixStack, IRenderTypeBuffer buffer, int light, int overlay) { public void render(PipeBlockEntity tile, float partialTicks, PoseStack matrixStack, MultiBufferSource source, int light, int overlay) {
if (!tile.getItems().isEmpty()) { if (!tile.getItems().isEmpty()) {
matrixStack.push(); matrixStack.pushPose();
BlockPos tilePos = tile.getPos(); var tilePos = tile.getBlockPos();
matrixStack.translate(-tilePos.getX(), -tilePos.getY(), -tilePos.getZ()); matrixStack.translate(-tilePos.getX(), -tilePos.getY(), -tilePos.getZ());
for (IPipeItem item : tile.getItems()) { for (var item : tile.getItems()) {
matrixStack.push(); matrixStack.pushPose();
item.render(tile, matrixStack, this.random, partialTicks, light, overlay, buffer); item.render(tile, matrixStack, this.random, partialTicks, light, overlay, source);
matrixStack.pop(); matrixStack.popPose();
} }
matrixStack.pop(); matrixStack.popPose();
} }
if (tile.cover != null) { if (tile.cover != null) {
matrixStack.push(); matrixStack.pushPose();
BlockModelRenderer.enableCache(); ForgeBlockModelRenderer.enableCaching();
for (RenderType layer : RenderType.getBlockRenderTypes()) { var renderer = Minecraft.getInstance().getBlockRenderer();
if (!RenderTypeLookup.canRenderInLayer(tile.cover, layer)) for (var layer : RenderType.chunkBufferLayers()) {
if (!ItemBlockRenderTypes.canRenderInLayer(tile.cover, layer))
continue; continue;
ForgeHooksClient.setRenderLayer(layer); ForgeHooksClient.setRenderType(layer);
Minecraft.getInstance().getBlockRendererDispatcher().renderBlock(tile.cover, matrixStack, buffer, light, overlay, EmptyModelData.INSTANCE); renderer.getModelRenderer().tesselateBlock(tile.getLevel(), renderer.getBlockModel(tile.cover), tile.cover, tile.getBlockPos(), matrixStack, source.getBuffer(layer), true, new Random(), tile.cover.getSeed(tile.getBlockPos()), overlay);
} }
ForgeHooksClient.setRenderLayer(null); ForgeHooksClient.setRenderType(null);
BlockModelRenderer.disableCache(); ForgeBlockModelRenderer.clearCache();
matrixStack.pop(); matrixStack.popPose();
} }
} }
} }

View file

@ -3,28 +3,28 @@ package de.ellpeck.prettypipes.pipe.containers;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.misc.FilterSlot; import de.ellpeck.prettypipes.misc.FilterSlot;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.inventory.container.ClickType; import net.minecraft.world.entity.player.Player;
import net.minecraft.inventory.container.Container; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.world.inventory.ClickType;
import net.minecraft.inventory.container.Slot; import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public abstract class AbstractPipeContainer<T extends IModule> extends Container { public abstract class AbstractPipeContainer<T extends IModule> extends AbstractContainerMenu {
public final PipeTileEntity tile; public final PipeBlockEntity tile;
public final T module; public final T module;
public final int moduleIndex; public final int moduleIndex;
public final ItemStack moduleStack; public final ItemStack moduleStack;
public AbstractPipeContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, BlockPos pos, int moduleIndex) { public AbstractPipeContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos, int moduleIndex) {
super(type, id); super(type, id);
this.tile = Utility.getBlockEntity(PipeTileEntity.class, player.world, pos); this.tile = Utility.getBlockEntity(PipeBlockEntity.class, player.level, pos);
this.moduleStack = moduleIndex < 0 ? null : this.tile.modules.getStackInSlot(moduleIndex); this.moduleStack = moduleIndex < 0 ? null : this.tile.modules.getStackInSlot(moduleIndex);
this.module = moduleIndex < 0 ? null : (T) this.moduleStack.getItem(); this.module = moduleIndex < 0 ? null : (T) this.moduleStack.getItem();
this.moduleIndex = moduleIndex; this.moduleIndex = moduleIndex;
@ -32,23 +32,18 @@ public abstract class AbstractPipeContainer<T extends IModule> extends Container
// needs to be done here so transferStackInSlot works correctly, bleh // needs to be done here so transferStackInSlot works correctly, bleh
this.addSlots(); this.addSlots();
for (int l = 0; l < 3; ++l) for (var l = 0; l < 3; ++l)
for (int j1 = 0; j1 < 9; ++j1) for (var j1 = 0; j1 < 9; ++j1)
this.addSlot(new Slot(player.inventory, j1 + l * 9 + 9, 8 + j1 * 18, 89 + l * 18 + 32)); this.addSlot(new Slot(player.getInventory(), j1 + l * 9 + 9, 8 + j1 * 18, 89 + l * 18 + 32));
for (int i1 = 0; i1 < 9; ++i1) for (var i1 = 0; i1 < 9; ++i1)
this.addSlot(new Slot(player.inventory, i1, 8 + i1 * 18, 147 + 32)); this.addSlot(new Slot(player.getInventory(), i1, 8 + i1 * 18, 147 + 32));
} }
protected abstract void addSlots(); protected abstract void addSlots();
@Override @Override
public boolean canInteractWith(PlayerEntity playerIn) { public ItemStack quickMoveStack(Player player, int slotIndex) {
return true; return Utility.transferStackInSlot(this, this::moveItemStackTo, player, slotIndex, stack -> {
}
@Override
public ItemStack transferStackInSlot(PlayerEntity player, int slotIndex) {
return Utility.transferStackInSlot(this, this::mergeItemStack, player, slotIndex, stack -> {
if (stack.getItem() instanceof IModule) if (stack.getItem() instanceof IModule)
return Pair.of(0, 3); return Pair.of(0, 3);
return null; return null;
@ -56,9 +51,14 @@ public abstract class AbstractPipeContainer<T extends IModule> extends Container
} }
@Override @Override
public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) { public void clicked(int slotId, int dragType, ClickType clickTypeIn, Player player) {
if (FilterSlot.checkFilter(this, slotId, player)) if (FilterSlot.checkFilter(this, slotId, player))
return ItemStack.EMPTY; return;
return super.slotClick(slotId, dragType, clickTypeIn, player); super.clicked(slotId, dragType, clickTypeIn, player);
}
@Override
public boolean stillValid(Player player) {
return true;
} }
} }

View file

@ -1,34 +1,34 @@
package de.ellpeck.prettypipes.pipe.containers; package de.ellpeck.prettypipes.pipe.containers;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.vertex.PoseStack;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.packets.PacketButton; import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler; import de.ellpeck.prettypipes.packets.PacketHandler;
import net.minecraft.client.audio.SimpleSound; import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.screen.inventory.ContainerScreen; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.widget.Widget; import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.network.chat.Component;
import net.minecraft.inventory.container.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.SoundEvents; import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.SlotItemHandler;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public abstract class AbstractPipeGui<T extends AbstractPipeContainer<?>> extends ContainerScreen<T> { public abstract class AbstractPipeGui<T extends AbstractPipeContainer<?>> extends AbstractContainerScreen<T> {
protected static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/pipe.png"); protected static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/pipe.png");
private final List<Tab> tabs = new ArrayList<>(); private final List<Tab> tabs = new ArrayList<>();
private final ItemStack[] lastItems = new ItemStack[this.container.tile.modules.getSlots()]; private final ItemStack[] lastItems = new ItemStack[this.menu.tile.modules.getSlots()];
public AbstractPipeGui(T screenContainer, PlayerInventory inv, ITextComponent titleIn) { public AbstractPipeGui(T screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
this.xSize = 176; this.imageWidth = 176;
this.ySize = 171 + 32; this.imageHeight = 171 + 32;
} }
@Override @Override
@ -38,12 +38,12 @@ public abstract class AbstractPipeGui<T extends AbstractPipeContainer<?>> extend
} }
@Override @Override
public void tick() { public void containerTick() {
super.tick(); super.containerTick();
boolean changed = false; var changed = false;
for (int i = 0; i < this.container.tile.modules.getSlots(); i++) { for (var i = 0; i < this.menu.tile.modules.getSlots(); i++) {
ItemStack stack = this.container.tile.modules.getStackInSlot(i); var stack = this.menu.tile.modules.getStackInSlot(i);
if (stack != this.lastItems[i]) { if (stack != this.lastItems[i]) {
this.lastItems[i] = stack; this.lastItems[i] = stack;
changed = true; changed = true;
@ -54,43 +54,44 @@ public abstract class AbstractPipeGui<T extends AbstractPipeContainer<?>> extend
} }
@Override @Override
public void render(MatrixStack matrix, int mouseX, int mouseY, float partialTicks) { public void render(PoseStack matrix, int mouseX, int mouseY, float partialTicks) {
this.renderBackground(matrix); this.renderBackground(matrix);
super.render(matrix, mouseX, mouseY, partialTicks); super.render(matrix, mouseX, mouseY, partialTicks);
for (Widget widget : this.buttons) { for (var widget : this.renderables) {
if (widget.isHovered()) if (widget instanceof AbstractWidget abstractWidget) {
widget.renderToolTip(matrix, mouseX, mouseY); if (abstractWidget.isHoveredOrFocused())
abstractWidget.renderToolTip(matrix, mouseX, mouseY);
}
} }
this.func_230459_a_(matrix, mouseX, mouseY); this.renderTooltip(matrix, mouseX, mouseY);
} }
@Override @Override
protected void drawGuiContainerForegroundLayer(MatrixStack matrix, int mouseX, int mouseY) { protected void renderLabels(PoseStack matrix, int mouseX, int mouseY) {
this.font.drawString(matrix, this.playerInventory.getDisplayName().getString(), 8, this.ySize - 96 + 2, 4210752); this.font.draw(matrix, this.playerInventoryTitle.getString(), 8, this.imageHeight - 96 + 2, 4210752);
this.font.drawString(matrix, this.title.getString(), 8, 6 + 32, 4210752); this.font.draw(matrix, this.title.getString(), 8, 6 + 32, 4210752);
for (Tab tab : this.tabs) for (var tab : this.tabs)
tab.drawForeground(matrix, mouseX, mouseY); tab.drawForeground(matrix, mouseX, mouseY);
} }
@Override @Override
protected void drawGuiContainerBackgroundLayer(MatrixStack matrix, float partialTicks, int mouseX, int mouseY) { protected void renderBg(PoseStack matrix, float partialTicks, int mouseX, int mouseY) {
this.getMinecraft().getTextureManager().bindTexture(TEXTURE); this.getMinecraft().getTextureManager().bindForSetup(TEXTURE);
this.blit(matrix, this.guiLeft, this.guiTop + 32, 0, 0, 176, 171); this.blit(matrix, this.leftPos, this.topPos + 32, 0, 0, 176, 171);
for (Tab tab : this.tabs) for (var tab : this.tabs)
tab.draw(matrix); tab.draw(matrix);
// draw the slots since we're using a blank ui // draw the slots since we're using a blank ui
for (Slot slot : this.container.inventorySlots) { for (var slot : this.menu.slots) {
if (slot.inventory == this.playerInventory) if (slot instanceof SlotItemHandler)
continue; this.blit(matrix, this.leftPos + slot.x - 1, this.topPos + slot.y - 1, 176, 62, 18, 18);
this.blit(matrix, this.guiLeft + slot.xPos - 1, this.guiTop + slot.yPos - 1, 176, 62, 18, 18);
} }
} }
@Override @Override
public boolean mouseClicked(double x, double y, int button) { public boolean mouseClicked(double x, double y, int button) {
for (Tab tab : this.tabs) { for (var tab : this.tabs) {
if (tab.onClicked(x, y, button)) if (tab.onClicked(x, y, button))
return true; return true;
} }
@ -100,17 +101,18 @@ public abstract class AbstractPipeGui<T extends AbstractPipeContainer<?>> extend
private void initTabs() { private void initTabs() {
this.tabs.clear(); this.tabs.clear();
this.tabs.add(new Tab(new ItemStack(Registry.pipeBlock), 0, -1)); this.tabs.add(new Tab(new ItemStack(Registry.pipeBlock), 0, -1));
for (int i = 0; i < this.container.tile.modules.getSlots(); i++) { for (var i = 0; i < this.menu.tile.modules.getSlots(); i++) {
ItemStack stack = this.container.tile.modules.getStackInSlot(i); var stack = this.menu.tile.modules.getStackInSlot(i);
if (stack.isEmpty()) if (stack.isEmpty())
continue; continue;
IModule module = (IModule) stack.getItem(); var module = (IModule) stack.getItem();
if (module.hasContainer(stack, this.container.tile)) if (module.hasContainer(stack, this.menu.tile))
this.tabs.add(new Tab(stack, this.tabs.size(), i)); this.tabs.add(new Tab(stack, this.tabs.size(), i));
} }
} }
private class Tab { private class Tab {
private final ItemStack moduleStack; private final ItemStack moduleStack;
private final int index; private final int index;
private final int x; private final int x;
@ -119,16 +121,16 @@ public abstract class AbstractPipeGui<T extends AbstractPipeContainer<?>> extend
public Tab(ItemStack moduleStack, int tabIndex, int index) { public Tab(ItemStack moduleStack, int tabIndex, int index) {
this.moduleStack = moduleStack; this.moduleStack = moduleStack;
this.index = index; this.index = index;
this.x = AbstractPipeGui.this.guiLeft + 5 + tabIndex * 28; this.x = AbstractPipeGui.this.leftPos + 5 + tabIndex * 28;
this.y = AbstractPipeGui.this.guiTop; this.y = AbstractPipeGui.this.topPos;
} }
private void draw(MatrixStack matrix) { private void draw(PoseStack matrix) {
int y = 2; var y = 2;
int v = 0; var v = 0;
int height = 30; var height = 30;
int itemOffset = 9; var itemOffset = 9;
if (this.index == AbstractPipeGui.this.container.moduleIndex) { if (this.index == AbstractPipeGui.this.menu.moduleIndex) {
y = 0; y = 0;
v = 30; v = 30;
height = 32; height = 32;
@ -136,25 +138,25 @@ public abstract class AbstractPipeGui<T extends AbstractPipeContainer<?>> extend
} }
AbstractPipeGui.this.blit(matrix, this.x, this.y + y, 176, v, 28, height); AbstractPipeGui.this.blit(matrix, this.x, this.y + y, 176, v, 28, height);
AbstractPipeGui.this.itemRenderer.renderItemIntoGUI(this.moduleStack, this.x + 6, this.y + itemOffset); AbstractPipeGui.this.itemRenderer.renderGuiItem(this.moduleStack, this.x + 6, this.y + itemOffset);
AbstractPipeGui.this.getMinecraft().getTextureManager().bindTexture(TEXTURE); AbstractPipeGui.this.getMinecraft().getTextureManager().bindForSetup(TEXTURE);
} }
private void drawForeground(MatrixStack matrix, int mouseX, int mouseY) { private void drawForeground(PoseStack matrix, int mouseX, int mouseY) {
if (mouseX < this.x || mouseY < this.y || mouseX >= this.x + 28 || mouseY >= this.y + 32) if (mouseX < this.x || mouseY < this.y || mouseX >= this.x + 28 || mouseY >= this.y + 32)
return; return;
AbstractPipeGui.this.renderTooltip(matrix, this.moduleStack.getDisplayName(), mouseX - AbstractPipeGui.this.guiLeft, mouseY - AbstractPipeGui.this.guiTop); AbstractPipeGui.this.renderTooltip(matrix, this.moduleStack.getDisplayName(), mouseX - AbstractPipeGui.this.leftPos, mouseY - AbstractPipeGui.this.topPos);
} }
private boolean onClicked(double mouseX, double mouseY, int button) { private boolean onClicked(double mouseX, double mouseY, int button) {
if (this.index == AbstractPipeGui.this.container.moduleIndex) if (this.index == AbstractPipeGui.this.menu.moduleIndex)
return false; return false;
if (button != 0) if (button != 0)
return false; return false;
if (mouseX < this.x || mouseY < this.y || mouseX >= this.x + 28 || mouseY >= this.y + 32) if (mouseX < this.x || mouseY < this.y || mouseX >= this.x + 28 || mouseY >= this.y + 32)
return false; return false;
PacketHandler.sendToServer(new PacketButton(AbstractPipeGui.this.container.tile.getPos(), PacketButton.ButtonResult.PIPE_TAB, this.index)); PacketHandler.sendToServer(new PacketButton(AbstractPipeGui.this.menu.tile.getBlockPos(), PacketButton.ButtonResult.PIPE_TAB, this.index));
AbstractPipeGui.this.getMinecraft().getSoundHandler().play(SimpleSound.master(SoundEvents.UI_BUTTON_CLICK, 1)); AbstractPipeGui.this.getMinecraft().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1));
return true; return true;
} }
} }

View file

@ -1,21 +1,22 @@
package de.ellpeck.prettypipes.pipe.containers; package de.ellpeck.prettypipes.pipe.containers;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.world.entity.player.Player;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.inventory.MenuType;
import net.minecraftforge.items.SlotItemHandler; import net.minecraftforge.items.SlotItemHandler;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class MainPipeContainer extends AbstractPipeContainer<IModule> { public class MainPipeContainer extends AbstractPipeContainer<IModule> {
public MainPipeContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, BlockPos pos) {
public MainPipeContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos) {
super(type, id, player, pos, -1); super(type, id, player, pos, -1);
} }
@Override @Override
protected void addSlots() { protected void addSlots() {
for (int i = 0; i < 3; i++) for (var i = 0; i < 3; i++)
this.addSlot(new SlotItemHandler(this.tile.modules, i, 62 + i * 18, 17 + 32)); this.addSlot(new SlotItemHandler(this.tile.modules, i, 62 + i * 18, 17 + 32));
} }
} }

View file

@ -1,10 +1,11 @@
package de.ellpeck.prettypipes.pipe.containers; package de.ellpeck.prettypipes.pipe.containers;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.network.chat.Component;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Inventory;
public class MainPipeGui extends AbstractPipeGui<MainPipeContainer> { public class MainPipeGui extends AbstractPipeGui<MainPipeContainer> {
public MainPipeGui(MainPipeContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
public MainPipeGui(MainPipeContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
} }
} }

View file

@ -3,7 +3,7 @@ package de.ellpeck.prettypipes.pipe.modules;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem; import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.items.ModuleTier; import de.ellpeck.prettypipes.items.ModuleTier;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
public class HighPriorityModuleItem extends ModuleItem { public class HighPriorityModuleItem extends ModuleItem {
@ -15,17 +15,17 @@ public class HighPriorityModuleItem extends ModuleItem {
} }
@Override @Override
public int getPriority(ItemStack module, PipeTileEntity tile) { public int getPriority(ItemStack module, PipeBlockEntity tile) {
return this.priority; return this.priority;
} }
@Override @Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) { public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
return !(other instanceof HighPriorityModuleItem) && !(other instanceof LowPriorityModuleItem); return !(other instanceof HighPriorityModuleItem) && !(other instanceof LowPriorityModuleItem);
} }
@Override @Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) { public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
return false; return false;
} }
} }

View file

@ -3,7 +3,7 @@ package de.ellpeck.prettypipes.pipe.modules;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem; import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.items.ModuleTier; import de.ellpeck.prettypipes.items.ModuleTier;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
public class LowPriorityModuleItem extends ModuleItem { public class LowPriorityModuleItem extends ModuleItem {
@ -15,17 +15,17 @@ public class LowPriorityModuleItem extends ModuleItem {
} }
@Override @Override
public int getPriority(ItemStack module, PipeTileEntity tile) { public int getPriority(ItemStack module, PipeBlockEntity tile) {
return this.priority; return this.priority;
} }
@Override @Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) { public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
return !(other instanceof LowPriorityModuleItem) && !(other instanceof HighPriorityModuleItem); return !(other instanceof LowPriorityModuleItem) && !(other instanceof HighPriorityModuleItem);
} }
@Override @Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) { public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
return false; return false;
} }
} }

View file

@ -2,7 +2,7 @@ package de.ellpeck.prettypipes.pipe.modules;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem; import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
public class RedstoneModuleItem extends ModuleItem { public class RedstoneModuleItem extends ModuleItem {
@ -13,17 +13,17 @@ public class RedstoneModuleItem extends ModuleItem {
} }
@Override @Override
public boolean canPipeWork(ItemStack module, PipeTileEntity tile) { public boolean canPipeWork(ItemStack module, PipeBlockEntity tile) {
return !tile.getWorld().isBlockPowered(tile.getPos()); return !tile.getLevel().hasNeighborSignal(tile.getBlockPos());
} }
@Override @Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) { public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
return other != this; return other != this;
} }
@Override @Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) { public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
return false; return false;
} }
} }

View file

@ -2,9 +2,9 @@ package de.ellpeck.prettypipes.pipe.modules;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem; import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import java.util.List; import java.util.List;
@ -19,25 +19,25 @@ public class SortingModuleItem extends ModuleItem {
} }
@Override @Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) { public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
return !(other instanceof SortingModuleItem); return !(other instanceof SortingModuleItem);
} }
@Override @Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) { public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
return false; return false;
} }
@Override @Override
public Integer getCustomNextNode(ItemStack module, PipeTileEntity tile, List<BlockPos> nodes, int index) { public Integer getCustomNextNode(ItemStack module, PipeBlockEntity tile, List<BlockPos> nodes, int index) {
switch (this.type) { switch (this.type) {
case ROUND_ROBIN: case ROUND_ROBIN:
// store an ever-increasing index and choose destinations based on that // store an ever-increasing index and choose destinations based on that
int next = module.hasTag() ? module.getTag().getInt("last") + 1 : 0; var next = module.hasTag() ? module.getTag().getInt("last") + 1 : 0;
module.getOrCreateTag().putInt("last", next); module.getOrCreateTag().putInt("last", next);
return next % nodes.size(); return next % nodes.size();
case RANDOM: case RANDOM:
return tile.getWorld().rand.nextInt(nodes.size()); return tile.getLevel().random.nextInt(nodes.size());
} }
return null; return null;
} }

View file

@ -3,7 +3,7 @@ package de.ellpeck.prettypipes.pipe.modules;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem; import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.items.ModuleTier; import de.ellpeck.prettypipes.items.ModuleTier;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
public class SpeedModuleItem extends ModuleItem { public class SpeedModuleItem extends ModuleItem {
@ -15,17 +15,17 @@ public class SpeedModuleItem extends ModuleItem {
} }
@Override @Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) { public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
return !(other instanceof SpeedModuleItem); return !(other instanceof SpeedModuleItem);
} }
@Override @Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) { public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
return false; return false;
} }
@Override @Override
public float getItemSpeedIncrease(ItemStack module, PipeTileEntity tile) { public float getItemSpeedIncrease(ItemStack module, PipeBlockEntity tile) {
return this.speedIncrease; return this.speedIncrease;
} }
} }

View file

@ -2,9 +2,9 @@ package de.ellpeck.prettypipes.pipe.modules.craft;
import de.ellpeck.prettypipes.misc.FilterSlot; import de.ellpeck.prettypipes.misc.FilterSlot;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.world.entity.player.Player;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.inventory.MenuType;
import net.minecraftforge.items.ItemStackHandler; import net.minecraftforge.items.ItemStackHandler;
public class CraftingModuleContainer extends AbstractPipeContainer<CraftingModuleItem> { public class CraftingModuleContainer extends AbstractPipeContainer<CraftingModuleItem> {
@ -13,29 +13,30 @@ public class CraftingModuleContainer extends AbstractPipeContainer<CraftingModul
public ItemStackHandler output; public ItemStackHandler output;
public boolean modified; public boolean modified;
public CraftingModuleContainer(ContainerType<?> type, int id, PlayerEntity player, BlockPos pos, int moduleIndex) { public CraftingModuleContainer(MenuType<?> type, int id, Player player, BlockPos pos, int moduleIndex) {
super(type, id, player, pos, moduleIndex); super(type, id, player, pos, moduleIndex);
} }
@Override @Override
protected void addSlots() { protected void addSlots() {
this.input = this.module.getInput(this.moduleStack); this.input = this.module.getInput(this.moduleStack);
for (int i = 0; i < this.input.getSlots(); i++) { for (var i = 0; i < this.input.getSlots(); i++) {
this.addSlot(new FilterSlot(this.input, i, (176 - this.module.inputSlots * 18) / 2 + 1 + i % 9 * 18, 17 + 32 + i / 9 * 18, false) { this.addSlot(new FilterSlot(this.input, i, (176 - this.module.inputSlots * 18) / 2 + 1 + i % 9 * 18, 17 + 32 + i / 9 * 18, false) {
@Override @Override
public void onSlotChanged() { public void setChanged() {
super.onSlotChanged(); super.setChanged();
CraftingModuleContainer.this.modified = true; CraftingModuleContainer.this.modified = true;
} }
}); });
} }
this.output = this.module.getOutput(this.moduleStack); this.output = this.module.getOutput(this.moduleStack);
for (int i = 0; i < this.output.getSlots(); i++) { for (var i = 0; i < this.output.getSlots(); i++) {
this.addSlot(new FilterSlot(this.output, i, (176 - this.module.outputSlots * 18) / 2 + 1 + i % 9 * 18, 85 + i / 9 * 18, false) { this.addSlot(new FilterSlot(this.output, i, (176 - this.module.outputSlots * 18) / 2 + 1 + i % 9 * 18, 85 + i / 9 * 18, false) {
@Override @Override
public void onSlotChanged() { public void setChanged() {
super.onSlotChanged(); super.setChanged();
CraftingModuleContainer.this.modified = true; CraftingModuleContainer.this.modified = true;
} }
}); });
@ -43,8 +44,8 @@ public class CraftingModuleContainer extends AbstractPipeContainer<CraftingModul
} }
@Override @Override
public void onContainerClosed(PlayerEntity playerIn) { public void removed(Player playerIn) {
super.onContainerClosed(playerIn); super.removed(playerIn);
if (this.modified) if (this.modified)
this.module.save(this.input, this.output, this.moduleStack); this.module.save(this.input, this.output, this.moduleStack);
} }

View file

@ -1,19 +1,20 @@
package de.ellpeck.prettypipes.pipe.modules.craft; package de.ellpeck.prettypipes.pipe.modules.craft;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.vertex.PoseStack;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.network.chat.Component;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Inventory;
public class CraftingModuleGui extends AbstractPipeGui<CraftingModuleContainer> { public class CraftingModuleGui extends AbstractPipeGui<CraftingModuleContainer> {
public CraftingModuleGui(CraftingModuleContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
public CraftingModuleGui(CraftingModuleContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
} }
@Override @Override
protected void drawGuiContainerBackgroundLayer(MatrixStack matrix, float partialTicks, int mouseX, int mouseY) { protected void renderBg(PoseStack matrix, float partialTicks, int mouseX, int mouseY) {
super.drawGuiContainerBackgroundLayer(matrix, partialTicks, mouseX, mouseY); super.renderBg(matrix, partialTicks, mouseX, mouseY);
this.getMinecraft().getTextureManager().bindTexture(TEXTURE); this.getMinecraft().getTextureManager().bindForSetup(TEXTURE);
this.blit(matrix, this.guiLeft + 176 / 2 - 16 / 2, this.guiTop + 32 + 18 * 2, 176, 80, 16, 16); this.blit(matrix, this.leftPos + 176 / 2 - 16 / 2, this.topPos + 32 + 18 * 2, 176, 80, 16, 16);
} }
} }

View file

@ -6,19 +6,17 @@ import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.items.ModuleTier; import de.ellpeck.prettypipes.items.ModuleTier;
import de.ellpeck.prettypipes.misc.ItemEquality; import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.network.NetworkLocation;
import de.ellpeck.prettypipes.network.NetworkLock; import de.ellpeck.prettypipes.network.NetworkLock;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import de.ellpeck.prettypipes.terminal.CraftingTerminalTileEntity; import de.ellpeck.prettypipes.terminal.CraftingTerminalBlockEntity;
import de.ellpeck.prettypipes.terminal.ItemTerminalTileEntity; import de.ellpeck.prettypipes.terminal.ItemTerminalBlockEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.items.ItemStackHandler; import net.minecraftforge.items.ItemStackHandler;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
@ -41,51 +39,51 @@ public class CraftingModuleItem extends ModuleItem {
} }
@Override @Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) { public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
return true; return true;
} }
@Override @Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) { public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
return true; return true;
} }
@Override @Override
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) { public AbstractPipeContainer<?> getContainer(ItemStack module, PipeBlockEntity tile, int windowId, Inventory inv, Player player, int moduleIndex) {
return new CraftingModuleContainer(Registry.craftingModuleContainer, windowId, player, tile.getPos(), moduleIndex); return new CraftingModuleContainer(Registry.craftingModuleContainer, windowId, player, tile.getBlockPos(), moduleIndex);
} }
@Override @Override
public boolean canNetworkSee(ItemStack module, PipeTileEntity tile) { public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile) {
return false; return false;
} }
@Override @Override
public boolean canAcceptItem(ItemStack module, PipeTileEntity tile, ItemStack stack) { public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack) {
return false; return false;
} }
@Override @Override
public void tick(ItemStack module, PipeTileEntity tile) { public void tick(ItemStack module, PipeBlockEntity tile) {
if (!tile.shouldWorkNow(this.speed) || !tile.canWork()) if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
return; return;
PipeNetwork network = PipeNetwork.get(tile.getWorld()); var network = PipeNetwork.get(tile.getLevel());
// process crafting ingredient requests // process crafting ingredient requests
if (!tile.craftIngredientRequests.isEmpty()) { if (!tile.craftIngredientRequests.isEmpty()) {
network.startProfile("crafting_ingredients"); network.startProfile("crafting_ingredients");
NetworkLock request = tile.craftIngredientRequests.peek(); var request = tile.craftIngredientRequests.peek();
ItemEquality[] equalityTypes = ItemFilter.getEqualityTypes(tile); var equalityTypes = ItemFilter.getEqualityTypes(tile);
Pair<BlockPos, ItemStack> dest = tile.getAvailableDestination(request.stack, true, true); var dest = tile.getAvailableDestination(request.stack, true, true);
if (dest != null) { if (dest != null) {
ItemStack requestRemain = network.requestExistingItem(request.location, tile.getPos(), dest.getLeft(), request, dest.getRight(), equalityTypes); var requestRemain = network.requestExistingItem(request.location, tile.getBlockPos(), dest.getLeft(), request, dest.getRight(), equalityTypes);
network.resolveNetworkLock(request); network.resolveNetworkLock(request);
tile.craftIngredientRequests.remove(); tile.craftIngredientRequests.remove();
// if we couldn't fit all items into the destination, create another request for the rest // if we couldn't fit all items into the destination, create another request for the rest
ItemStack remain = request.stack.copy(); var remain = request.stack.copy();
remain.shrink(dest.getRight().getCount() - requestRemain.getCount()); remain.shrink(dest.getRight().getCount() - requestRemain.getCount());
if (!remain.isEmpty()) { if (!remain.isEmpty()) {
NetworkLock remainRequest = new NetworkLock(request.location, remain); var remainRequest = new NetworkLock(request.location, remain);
tile.craftIngredientRequests.add(remainRequest); tile.craftIngredientRequests.add(remainRequest);
network.createNetworkLock(remainRequest); network.createNetworkLock(remainRequest);
} }
@ -95,17 +93,17 @@ public class CraftingModuleItem extends ModuleItem {
// pull requested crafting results from the network once they are stored // pull requested crafting results from the network once they are stored
if (!tile.craftResultRequests.isEmpty()) { if (!tile.craftResultRequests.isEmpty()) {
network.startProfile("crafting_results"); network.startProfile("crafting_results");
List<NetworkLocation> items = network.getOrderedNetworkItems(tile.getPos()); var items = network.getOrderedNetworkItems(tile.getBlockPos());
ItemEquality[] equalityTypes = ItemFilter.getEqualityTypes(tile); var equalityTypes = ItemFilter.getEqualityTypes(tile);
for (Pair<BlockPos, ItemStack> request : tile.craftResultRequests) { for (var request : tile.craftResultRequests) {
ItemStack remain = request.getRight().copy(); var remain = request.getRight().copy();
PipeTileEntity destPipe = network.getPipe(request.getLeft()); var destPipe = network.getPipe(request.getLeft());
if (destPipe != null) { if (destPipe != null) {
Pair<BlockPos, ItemStack> dest = destPipe.getAvailableDestinationOrConnectable(remain, true, true); var dest = destPipe.getAvailableDestinationOrConnectable(remain, true, true);
if (dest == null) if (dest == null)
continue; continue;
for (NetworkLocation item : items) { for (var item : items) {
ItemStack requestRemain = network.requestExistingItem(item, request.getLeft(), dest.getLeft(), null, dest.getRight(), equalityTypes); var requestRemain = network.requestExistingItem(item, request.getLeft(), dest.getLeft(), null, dest.getRight(), equalityTypes);
remain.shrink(dest.getRight().getCount() - requestRemain.getCount()); remain.shrink(dest.getRight().getCount() - requestRemain.getCount());
if (remain.isEmpty()) if (remain.isEmpty())
break; break;
@ -125,11 +123,11 @@ public class CraftingModuleItem extends ModuleItem {
} }
@Override @Override
public List<ItemStack> getAllCraftables(ItemStack module, PipeTileEntity tile) { public List<ItemStack> getAllCraftables(ItemStack module, PipeBlockEntity tile) {
List<ItemStack> ret = new ArrayList<>(); List<ItemStack> ret = new ArrayList<>();
ItemStackHandler output = this.getOutput(module); var output = this.getOutput(module);
for (int i = 0; i < output.getSlots(); i++) { for (var i = 0; i < output.getSlots(); i++) {
ItemStack stack = output.getStackInSlot(i); var stack = output.getStackInSlot(i);
if (!stack.isEmpty()) if (!stack.isEmpty())
ret.add(stack); ret.add(stack);
} }
@ -137,19 +135,19 @@ public class CraftingModuleItem extends ModuleItem {
} }
@Override @Override
public int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) { public int getCraftableAmount(ItemStack module, PipeBlockEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
PipeNetwork network = PipeNetwork.get(tile.getWorld()); var network = PipeNetwork.get(tile.getLevel());
List<NetworkLocation> items = network.getOrderedNetworkItems(tile.getPos()); var items = network.getOrderedNetworkItems(tile.getBlockPos());
ItemEquality[] equalityTypes = ItemFilter.getEqualityTypes(tile); var equalityTypes = ItemFilter.getEqualityTypes(tile);
ItemStackHandler input = this.getInput(module); var input = this.getInput(module);
int craftable = 0; var craftable = 0;
ItemStackHandler output = this.getOutput(module); var output = this.getOutput(module);
for (int i = 0; i < output.getSlots(); i++) { for (var i = 0; i < output.getSlots(); i++) {
ItemStack out = output.getStackInSlot(i); var out = output.getStackInSlot(i);
if (!out.isEmpty() && ItemEquality.compareItems(out, stack, equalityTypes)) { if (!out.isEmpty() && ItemEquality.compareItems(out, stack, equalityTypes)) {
// figure out how many crafting operations we can actually do with the input items we have in the network // figure out how many crafting operations we can actually do with the input items we have in the network
int availableCrafts = CraftingTerminalTileEntity.getAvailableCrafts(tile, input.getSlots(), input::getStackInSlot, k -> true, s -> items, unavailableConsumer, addDependency(dependencyChain, module), equalityTypes); var availableCrafts = CraftingTerminalBlockEntity.getAvailableCrafts(tile, input.getSlots(), input::getStackInSlot, k -> true, s -> items, unavailableConsumer, addDependency(dependencyChain, module), equalityTypes);
if (availableCrafts > 0) if (availableCrafts > 0)
craftable += out.getCount() * availableCrafts; craftable += out.getCount() * availableCrafts;
} }
@ -158,35 +156,35 @@ public class CraftingModuleItem extends ModuleItem {
} }
@Override @Override
public ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) { public ItemStack craft(ItemStack module, PipeBlockEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
// check if we can craft the required amount of items // check if we can craft the required amount of items
int craftableAmount = this.getCraftableAmount(module, tile, unavailableConsumer, stack, dependencyChain); var craftableAmount = this.getCraftableAmount(module, tile, unavailableConsumer, stack, dependencyChain);
if (craftableAmount <= 0) if (craftableAmount <= 0)
return stack; return stack;
PipeNetwork network = PipeNetwork.get(tile.getWorld()); var network = PipeNetwork.get(tile.getLevel());
List<NetworkLocation> items = network.getOrderedNetworkItems(tile.getPos()); var items = network.getOrderedNetworkItems(tile.getBlockPos());
ItemEquality[] equalityTypes = ItemFilter.getEqualityTypes(tile); var equalityTypes = ItemFilter.getEqualityTypes(tile);
int resultAmount = this.getResultAmountPerCraft(module, stack, equalityTypes); var resultAmount = this.getResultAmountPerCraft(module, stack, equalityTypes);
int requiredCrafts = MathHelper.ceil(stack.getCount() / (float) resultAmount); var requiredCrafts = Mth.ceil(stack.getCount() / (float) resultAmount);
int toCraft = Math.min(craftableAmount, requiredCrafts); var toCraft = Math.min(craftableAmount, requiredCrafts);
ItemStackHandler input = this.getInput(module); var input = this.getInput(module);
for (int i = 0; i < input.getSlots(); i++) { for (var i = 0; i < input.getSlots(); i++) {
ItemStack in = input.getStackInSlot(i); var in = input.getStackInSlot(i);
if (in.isEmpty()) if (in.isEmpty())
continue; continue;
ItemStack copy = in.copy(); var copy = in.copy();
copy.setCount(in.getCount() * toCraft); copy.setCount(in.getCount() * toCraft);
Pair<List<NetworkLock>, ItemStack> ret = ItemTerminalTileEntity.requestItemLater(tile.getWorld(), tile.getPos(), items, unavailableConsumer, copy, addDependency(dependencyChain, module), equalityTypes); var ret = ItemTerminalBlockEntity.requestItemLater(tile.getLevel(), tile.getBlockPos(), items, unavailableConsumer, copy, addDependency(dependencyChain, module), equalityTypes);
tile.craftIngredientRequests.addAll(ret.getLeft()); tile.craftIngredientRequests.addAll(ret.getLeft());
} }
ItemStack remain = stack.copy(); var remain = stack.copy();
remain.shrink(resultAmount * toCraft); remain.shrink(resultAmount * toCraft);
ItemStack result = stack.copy(); var result = stack.copy();
result.shrink(remain.getCount()); result.shrink(remain.getCount());
tile.craftResultRequests.add(Pair.of(destPipe, result)); tile.craftResultRequests.add(Pair.of(destPipe, result));
@ -194,21 +192,21 @@ public class CraftingModuleItem extends ModuleItem {
} }
public ItemStackHandler getInput(ItemStack module) { public ItemStackHandler getInput(ItemStack module) {
ItemStackHandler handler = new ItemStackHandler(this.inputSlots); var handler = new ItemStackHandler(this.inputSlots);
if (module.hasTag()) if (module.hasTag())
handler.deserializeNBT(module.getTag().getCompound("input")); handler.deserializeNBT(module.getTag().getCompound("input"));
return handler; return handler;
} }
public ItemStackHandler getOutput(ItemStack module) { public ItemStackHandler getOutput(ItemStack module) {
ItemStackHandler handler = new ItemStackHandler(this.outputSlots); var handler = new ItemStackHandler(this.outputSlots);
if (module.hasTag()) if (module.hasTag())
handler.deserializeNBT(module.getTag().getCompound("output")); handler.deserializeNBT(module.getTag().getCompound("output"));
return handler; return handler;
} }
public void save(ItemStackHandler input, ItemStackHandler output, ItemStack module) { public void save(ItemStackHandler input, ItemStackHandler output, ItemStack module) {
CompoundTag tag = module.getOrCreateTag(); var tag = module.getOrCreateTag();
if (input != null) if (input != null)
tag.put("input", input.serializeNBT()); tag.put("input", input.serializeNBT());
if (output != null) if (output != null)
@ -216,10 +214,10 @@ public class CraftingModuleItem extends ModuleItem {
} }
private int getResultAmountPerCraft(ItemStack module, ItemStack stack, ItemEquality... equalityTypes) { private int getResultAmountPerCraft(ItemStack module, ItemStack stack, ItemEquality... equalityTypes) {
ItemStackHandler output = this.getOutput(module); var output = this.getOutput(module);
int resultAmount = 0; var resultAmount = 0;
for (int i = 0; i < output.getSlots(); i++) { for (var i = 0; i < output.getSlots(); i++) {
ItemStack out = output.getStackInSlot(i); var out = output.getStackInSlot(i);
if (ItemEquality.compareItems(stack, out, equalityTypes)) if (ItemEquality.compareItems(stack, out, equalityTypes))
resultAmount += out.getCount(); resultAmount += out.getCount();
} }

View file

@ -3,10 +3,9 @@ package de.ellpeck.prettypipes.pipe.modules.extraction;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer; import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.world.entity.player.Player;
import net.minecraft.inventory.container.Slot; import net.minecraft.world.inventory.MenuType;
import net.minecraft.util.math.BlockPos;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -14,20 +13,20 @@ public class ExtractionModuleContainer extends AbstractPipeContainer<ExtractionM
public ItemFilter filter; public ItemFilter filter;
public ExtractionModuleContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, BlockPos pos, int moduleIndex) { public ExtractionModuleContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos, int moduleIndex) {
super(type, id, player, pos, moduleIndex); super(type, id, player, pos, moduleIndex);
} }
@Override @Override
protected void addSlots() { protected void addSlots() {
this.filter = this.module.getItemFilter(this.moduleStack, this.tile); this.filter = this.module.getItemFilter(this.moduleStack, this.tile);
for (Slot slot : this.filter.getSlots((176 - this.module.filterSlots * 18) / 2 + 1, 17 + 32)) for (var slot : this.filter.getSlots((176 - this.module.filterSlots * 18) / 2 + 1, 17 + 32))
this.addSlot(slot); this.addSlot(slot);
} }
@Override @Override
public void onContainerClosed(PlayerEntity playerIn) { public void removed(Player playerIn) {
super.onContainerClosed(playerIn); super.removed(playerIn);
this.filter.save(); this.filter.save();
} }

View file

@ -1,19 +1,19 @@
package de.ellpeck.prettypipes.pipe.modules.extraction; package de.ellpeck.prettypipes.pipe.modules.extraction;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui;
import net.minecraft.client.gui.widget.Widget; import net.minecraft.network.chat.Component;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.util.text.ITextComponent;
public class ExtractionModuleGui extends AbstractPipeGui<ExtractionModuleContainer> { public class ExtractionModuleGui extends AbstractPipeGui<ExtractionModuleContainer> {
public ExtractionModuleGui(ExtractionModuleContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
public ExtractionModuleGui(ExtractionModuleContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
} }
@Override @Override
protected void init() { protected void init() {
super.init(); super.init();
for (Widget widget : this.container.filter.getButtons(this, this.guiLeft + 7, this.guiTop + 17 + 32 + 20)) for (var widget : this.menu.filter.getButtons(this, this.leftPos + 7, this.topPos + 17 + 32 + 20))
this.addButton(widget); this.addRenderableWidget(widget);
} }
} }

View file

@ -6,13 +6,12 @@ import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.items.ModuleTier; import de.ellpeck.prettypipes.items.ModuleTier;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.Direction;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.util.Direction;
import net.minecraftforge.items.IItemHandler;
public class ExtractionModuleItem extends ModuleItem { public class ExtractionModuleItem extends ModuleItem {
@ -30,23 +29,23 @@ public class ExtractionModuleItem extends ModuleItem {
} }
@Override @Override
public void tick(ItemStack module, PipeTileEntity tile) { public void tick(ItemStack module, PipeBlockEntity tile) {
if (!tile.shouldWorkNow(this.speed) || !tile.canWork()) if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
return; return;
ItemFilter filter = this.getItemFilter(module, tile); var filter = this.getItemFilter(module, tile);
PipeNetwork network = PipeNetwork.get(tile.getWorld()); var network = PipeNetwork.get(tile.getLevel());
for (Direction dir : Direction.values()) { for (var dir : Direction.values()) {
IItemHandler handler = tile.getItemHandler(dir); var handler = tile.getItemHandler(dir);
if (handler == null) if (handler == null)
continue; continue;
for (int j = 0; j < handler.getSlots(); j++) { for (var j = 0; j < handler.getSlots(); j++) {
ItemStack stack = handler.extractItem(j, this.maxExtraction, true); var stack = handler.extractItem(j, this.maxExtraction, true);
if (stack.isEmpty()) if (stack.isEmpty())
continue; continue;
if (!filter.isAllowed(stack)) if (!filter.isAllowed(stack))
continue; continue;
ItemStack remain = network.routeItem(tile.getPos(), tile.getPos().offset(dir), stack, this.preventOversending); var remain = network.routeItem(tile.getBlockPos(), tile.getBlockPos().relative(dir), stack, this.preventOversending);
if (remain.getCount() != stack.getCount()) { if (remain.getCount() != stack.getCount()) {
handler.extractItem(j, stack.getCount() - remain.getCount(), false); handler.extractItem(j, stack.getCount() - remain.getCount(), false);
return; return;
@ -56,32 +55,32 @@ public class ExtractionModuleItem extends ModuleItem {
} }
@Override @Override
public boolean canNetworkSee(ItemStack module, PipeTileEntity tile) { public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile) {
return false; return false;
} }
@Override @Override
public boolean canAcceptItem(ItemStack module, PipeTileEntity tile, ItemStack stack) { public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack) {
return false; return false;
} }
@Override @Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) { public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
return !(other instanceof ExtractionModuleItem); return !(other instanceof ExtractionModuleItem);
} }
@Override @Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) { public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
return true; return true;
} }
@Override @Override
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) { public AbstractPipeContainer<?> getContainer(ItemStack module, PipeBlockEntity tile, int windowId, Inventory inv, Player player, int moduleIndex) {
return new ExtractionModuleContainer(Registry.extractionModuleContainer, windowId, player, tile.getPos(), moduleIndex); return new ExtractionModuleContainer(Registry.extractionModuleContainer, windowId, player, tile.getBlockPos(), moduleIndex);
} }
@Override @Override
public ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile) { public ItemFilter getItemFilter(ItemStack module, PipeBlockEntity tile) {
return new ItemFilter(this.filterSlots, module, tile); return new ItemFilter(this.filterSlots, module, tile);
} }
} }

View file

@ -2,29 +2,29 @@ package de.ellpeck.prettypipes.pipe.modules.filter;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.world.entity.player.Player;
import net.minecraft.inventory.container.Slot; import net.minecraft.world.inventory.MenuType;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.inventory.Slot;
public class FilterIncreaseModuleContainer extends AbstractPipeContainer<FilterIncreaseModuleItem> implements ItemFilter.IFilteredContainer { public class FilterIncreaseModuleContainer extends AbstractPipeContainer<FilterIncreaseModuleItem> implements ItemFilter.IFilteredContainer {
public ItemFilter filter; public ItemFilter filter;
public FilterIncreaseModuleContainer(ContainerType<?> type, int id, PlayerEntity player, BlockPos pos, int moduleIndex) { public FilterIncreaseModuleContainer(MenuType<?> type, int id, Player player, BlockPos pos, int moduleIndex) {
super(type, id, player, pos, moduleIndex); super(type, id, player, pos, moduleIndex);
} }
@Override @Override
protected void addSlots() { protected void addSlots() {
this.filter = this.module.getItemFilter(this.moduleStack, this.tile); this.filter = this.module.getItemFilter(this.moduleStack, this.tile);
for (Slot slot : this.filter.getSlots(8, 49)) for (var slot : this.filter.getSlots(8, 49))
this.addSlot(slot); this.addSlot(slot);
} }
@Override @Override
public void onContainerClosed(PlayerEntity playerIn) { public void removed(Player playerIn) {
super.onContainerClosed(playerIn); super.removed(playerIn);
this.filter.save(); this.filter.save();
} }

View file

@ -1,11 +1,12 @@
package de.ellpeck.prettypipes.pipe.modules.filter; package de.ellpeck.prettypipes.pipe.modules.filter;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.network.chat.Component;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Inventory;
public class FilterIncreaseModuleGui extends AbstractPipeGui<FilterIncreaseModuleContainer> { public class FilterIncreaseModuleGui extends AbstractPipeGui<FilterIncreaseModuleContainer> {
public FilterIncreaseModuleGui(FilterIncreaseModuleContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
public FilterIncreaseModuleGui(FilterIncreaseModuleContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
} }
} }

View file

@ -4,10 +4,10 @@ import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem; import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
public class FilterIncreaseModuleItem extends ModuleItem { public class FilterIncreaseModuleItem extends ModuleItem {
@ -18,23 +18,23 @@ public class FilterIncreaseModuleItem extends ModuleItem {
} }
@Override @Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) { public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
return true; return true;
} }
@Override @Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) { public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
return true; return true;
} }
@Override @Override
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) { public AbstractPipeContainer<?> getContainer(ItemStack module, PipeBlockEntity tile, int windowId, Inventory inv, Player player, int moduleIndex) {
return new FilterIncreaseModuleContainer(Registry.filterIncreaseModuleContainer, windowId, player, tile.getPos(), moduleIndex); return new FilterIncreaseModuleContainer(Registry.filterIncreaseModuleContainer, windowId, player, tile.getBlockPos(), moduleIndex);
} }
@Override @Override
public ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile) { public ItemFilter getItemFilter(ItemStack module, PipeBlockEntity tile) {
ItemFilter filter = new ItemFilter(18, module, tile); var filter = new ItemFilter(18, module, tile);
filter.canModifyWhitelist = false; filter.canModifyWhitelist = false;
return filter; return filter;
} }

View file

@ -3,10 +3,10 @@ package de.ellpeck.prettypipes.pipe.modules.insertion;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer; import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.world.entity.player.Player;
import net.minecraft.inventory.container.Slot; import net.minecraft.world.inventory.MenuType;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.inventory.Slot;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -14,20 +14,20 @@ public class FilterModuleContainer extends AbstractPipeContainer<FilterModuleIte
public ItemFilter filter; public ItemFilter filter;
public FilterModuleContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, BlockPos pos, int moduleIndex) { public FilterModuleContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos, int moduleIndex) {
super(type, id, player, pos, moduleIndex); super(type, id, player, pos, moduleIndex);
} }
@Override @Override
protected void addSlots() { protected void addSlots() {
this.filter = this.module.getItemFilter(this.moduleStack, this.tile); this.filter = this.module.getItemFilter(this.moduleStack, this.tile);
for (Slot slot : this.filter.getSlots((176 - Math.min(this.module.filterSlots, 9) * 18) / 2 + 1, 17 + 32)) for (var slot : this.filter.getSlots((176 - Math.min(this.module.filterSlots, 9) * 18) / 2 + 1, 17 + 32))
this.addSlot(slot); this.addSlot(slot);
} }
@Override @Override
public void onContainerClosed(PlayerEntity playerIn) { public void removed(Player playerIn) {
super.onContainerClosed(playerIn); super.removed(playerIn);
this.filter.save(); this.filter.save();
} }

View file

@ -1,20 +1,21 @@
package de.ellpeck.prettypipes.pipe.modules.insertion; package de.ellpeck.prettypipes.pipe.modules.insertion;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui;
import net.minecraft.client.gui.widget.Widget; import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.network.chat.Component;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.Mth;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Inventory;
public class FilterModuleGui extends AbstractPipeGui<FilterModuleContainer> { public class FilterModuleGui extends AbstractPipeGui<FilterModuleContainer> {
public FilterModuleGui(FilterModuleContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
public FilterModuleGui(FilterModuleContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
} }
@Override @Override
protected void init() { protected void init() {
super.init(); super.init();
for (Widget widget : this.container.filter.getButtons(this, this.guiLeft + 7, this.guiTop + 17 + 32 + 18 * MathHelper.ceil(this.container.filter.getSlots() / 9F) + 2)) for (var widget : this.menu.filter.getButtons(this, this.leftPos + 7, this.topPos + 17 + 32 + 18 * Mth.ceil(this.menu.filter.getSlots() / 9F) + 2))
this.addButton(widget); this.addRenderableWidget(widget);
} }
} }

View file

@ -5,10 +5,10 @@ import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem; import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.items.ModuleTier; import de.ellpeck.prettypipes.items.ModuleTier;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
public class FilterModuleItem extends ModuleItem { public class FilterModuleItem extends ModuleItem {
@ -23,29 +23,29 @@ public class FilterModuleItem extends ModuleItem {
} }
@Override @Override
public boolean canAcceptItem(ItemStack module, PipeTileEntity tile, ItemStack stack) { public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack) {
ItemFilter filter = this.getItemFilter(module, tile); var filter = this.getItemFilter(module, tile);
return filter.isAllowed(stack); return filter.isAllowed(stack);
} }
@Override @Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) { public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
return !(other instanceof FilterModuleItem); return !(other instanceof FilterModuleItem);
} }
@Override @Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) { public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
return true; return true;
} }
@Override @Override
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) { public AbstractPipeContainer<?> getContainer(ItemStack module, PipeBlockEntity tile, int windowId, Inventory inv, Player player, int moduleIndex) {
return new FilterModuleContainer(Registry.filterModuleContainer, windowId, player, tile.getPos(), moduleIndex); return new FilterModuleContainer(Registry.filterModuleContainer, windowId, player, tile.getBlockPos(), moduleIndex);
} }
@Override @Override
public ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile) { public ItemFilter getItemFilter(ItemStack module, PipeBlockEntity tile) {
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile); var filter = new ItemFilter(this.filterSlots, module, tile);
filter.canPopulateFromInventories = this.canPopulateFromInventories; filter.canPopulateFromInventories = this.canPopulateFromInventories;
return filter; return filter;
} }

View file

@ -2,11 +2,11 @@ package de.ellpeck.prettypipes.pipe.modules.modifier;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.HashSet; import java.util.HashSet;
@ -16,15 +16,15 @@ import java.util.stream.Collectors;
public class FilterModifierModuleContainer extends AbstractPipeContainer<FilterModifierModuleItem> { public class FilterModifierModuleContainer extends AbstractPipeContainer<FilterModifierModuleItem> {
public FilterModifierModuleContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, BlockPos pos, int moduleIndex) { public FilterModifierModuleContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos, int moduleIndex) {
super(type, id, player, pos, moduleIndex); super(type, id, player, pos, moduleIndex);
} }
public List<ResourceLocation> getTags() { public List<ResourceLocation> getTags() {
Set<ResourceLocation> unsortedTags = new HashSet<>(); Set<ResourceLocation> unsortedTags = new HashSet<>();
for (ItemFilter filter : this.tile.getFilters()) { for (var filter : this.tile.getFilters()) {
for (int i = 0; i < filter.getSlots(); i++) { for (var i = 0; i < filter.getSlots(); i++) {
ItemStack stack = filter.getStackInSlot(i); var stack = filter.getStackInSlot(i);
unsortedTags.addAll(stack.getItem().getTags()); unsortedTags.addAll(stack.getItem().getTags());
} }
} }

View file

@ -1,59 +1,60 @@
package de.ellpeck.prettypipes.pipe.modules.modifier; package de.ellpeck.prettypipes.pipe.modules.modifier;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.vertex.PoseStack;
import de.ellpeck.prettypipes.packets.PacketButton; import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui;
import net.minecraft.client.audio.SimpleSound; import net.minecraft.ChatFormatting;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.SoundEvents; import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.Mth;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.util.text.TextFormatting;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class FilterModifierModuleGui extends AbstractPipeGui<FilterModifierModuleContainer> { public class FilterModifierModuleGui extends AbstractPipeGui<FilterModifierModuleContainer> {
private int scrollOffset; private int scrollOffset;
private boolean isScrolling; private boolean isScrolling;
private List<ResourceLocation> tags; private List<ResourceLocation> tags;
private final List<Tag> tagButtons = new ArrayList<>(); private final List<Tag> tagButtons = new ArrayList<>();
public FilterModifierModuleGui(FilterModifierModuleContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) { public FilterModifierModuleGui(FilterModifierModuleContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
} }
@Override @Override
protected void drawGuiContainerBackgroundLayer(MatrixStack matrix, float partialTicks, int mouseX, int mouseY) { protected void renderBg(PoseStack matrix, float partialTicks, int mouseX, int mouseY) {
super.drawGuiContainerBackgroundLayer(matrix, partialTicks, mouseX, mouseY); super.renderBg(matrix, partialTicks, mouseX, mouseY);
this.blit(matrix, this.guiLeft + 7, this.guiTop + 32 + 15, 0, 196, 162, 60); this.blit(matrix, this.leftPos + 7, this.topPos + 32 + 15, 0, 196, 162, 60);
for (Tag tag : this.tagButtons) for (var tag : this.tagButtons)
tag.draw(matrix, mouseX, mouseY); tag.draw(matrix, mouseX, mouseY);
if (this.tags.size() >= 6) { if (this.tags.size() >= 6) {
float percentage = this.scrollOffset / (float) (this.tags.size() - 5); var percentage = this.scrollOffset / (float) (this.tags.size() - 5);
this.blit(matrix, this.guiLeft + 156, this.guiTop + 32 + 16 + (int) (percentage * (58 - 15)), 232, 241, 12, 15); this.blit(matrix, this.leftPos + 156, this.topPos + 32 + 16 + (int) (percentage * (58 - 15)), 232, 241, 12, 15);
} else { } else {
this.blit(matrix, this.guiLeft + 156, this.guiTop + 32 + 16, 244, 241, 12, 15); this.blit(matrix, this.leftPos + 156, this.topPos + 32 + 16, 244, 241, 12, 15);
} }
} }
@Override @Override
protected void init() { protected void init() {
super.init(); super.init();
this.tags = this.container.getTags(); this.tags = this.menu.getTags();
this.updateWidgets(); this.updateWidgets();
} }
@Override @Override
public boolean mouseClicked(double mouseX, double mouseY, int button) { public boolean mouseClicked(double mouseX, double mouseY, int button) {
for (Tag tag : this.tagButtons) { for (var tag : this.tagButtons) {
if (tag.onClicked(mouseX, mouseY, button)) if (tag.onClicked(mouseX, mouseY, button))
return true; return true;
} }
if (button == 0 && mouseX >= this.guiLeft + 156 && this.guiTop + mouseY >= 32 + 16 && mouseX < this.guiLeft + 156 + 12 && mouseY < this.guiTop + 32 + 16 + 58) { if (button == 0 && mouseX >= this.leftPos + 156 && this.topPos + mouseY >= 32 + 16 && mouseX < this.leftPos + 156 + 12 && mouseY < this.topPos + 32 + 16 + 58) {
this.isScrolling = true; this.isScrolling = true;
return true; return true;
} }
@ -70,8 +71,8 @@ public class FilterModifierModuleGui extends AbstractPipeGui<FilterModifierModul
@Override @Override
public boolean mouseDragged(double mouseX, double mouseY, int i, double j, double k) { public boolean mouseDragged(double mouseX, double mouseY, int i, double j, double k) {
if (this.isScrolling) { if (this.isScrolling) {
float percentage = MathHelper.clamp(((float) mouseY - (this.guiTop + 32 + 18)) / (58 - 15), 0, 1); var percentage = Mth.clamp(((float) mouseY - (this.topPos + 32 + 18)) / (58 - 15), 0, 1);
int offset = (int) (percentage * (float) (this.tags.size() - 5)); var offset = (int) (percentage * (float) (this.tags.size() - 5));
if (offset != this.scrollOffset) { if (offset != this.scrollOffset) {
this.scrollOffset = offset; this.scrollOffset = offset;
this.updateWidgets(); this.updateWidgets();
@ -84,7 +85,7 @@ public class FilterModifierModuleGui extends AbstractPipeGui<FilterModifierModul
@Override @Override
public boolean mouseScrolled(double x, double y, double scroll) { public boolean mouseScrolled(double x, double y, double scroll) {
if (this.tags.size() >= 6) { if (this.tags.size() >= 6) {
int offset = MathHelper.clamp(this.scrollOffset - (int) Math.signum(scroll), 0, this.tags.size() - 5); var offset = Mth.clamp(this.scrollOffset - (int) Math.signum(scroll), 0, this.tags.size() - 5);
if (offset != this.scrollOffset) { if (offset != this.scrollOffset) {
this.scrollOffset = offset; this.scrollOffset = offset;
this.updateWidgets(); this.updateWidgets();
@ -95,10 +96,10 @@ public class FilterModifierModuleGui extends AbstractPipeGui<FilterModifierModul
private void updateWidgets() { private void updateWidgets() {
this.tagButtons.clear(); this.tagButtons.clear();
for (int i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) {
if (i >= this.tags.size()) if (i >= this.tags.size())
break; break;
this.tagButtons.add(new Tag(this.tags.get(this.scrollOffset + i), this.guiLeft + 10, this.guiTop + 32 + 17 + i * 12)); this.tagButtons.add(new Tag(this.tags.get(this.scrollOffset + i), this.leftPos + 10, this.topPos + 32 + 17 + i * 12));
} }
} }
@ -114,15 +115,15 @@ public class FilterModifierModuleGui extends AbstractPipeGui<FilterModifierModul
this.y = y; this.y = y;
} }
private void draw(MatrixStack matrix, double mouseX, double mouseY) { private void draw(PoseStack matrix, double mouseX, double mouseY) {
int color = 4210752; var color = 4210752;
String text = this.tag.toString(); var text = this.tag.toString();
if (mouseX >= this.x && mouseY >= this.y && mouseX < this.x + 140 && mouseY < this.y + 12) if (mouseX >= this.x && mouseY >= this.y && mouseX < this.x + 140 && mouseY < this.y + 12)
color = 0xFFFFFF; color = 0xFFFFFF;
if (this.tag.equals(FilterModifierModuleItem.getFilterTag(FilterModifierModuleGui.this.container.moduleStack))) if (this.tag.equals(FilterModifierModuleItem.getFilterTag(FilterModifierModuleGui.this.menu.moduleStack)))
text = TextFormatting.BOLD + text; text = ChatFormatting.BOLD + text;
FilterModifierModuleGui.this.font.drawString(matrix, text, this.x, this.y, color); FilterModifierModuleGui.this.font.draw(matrix, text, this.x, this.y, color);
FilterModifierModuleGui.this.getMinecraft().getTextureManager().bindTexture(TEXTURE); FilterModifierModuleGui.this.getMinecraft().getTextureManager().bindForSetup(TEXTURE);
} }
private boolean onClicked(double mouseX, double mouseY, int button) { private boolean onClicked(double mouseX, double mouseY, int button) {
@ -130,8 +131,8 @@ public class FilterModifierModuleGui extends AbstractPipeGui<FilterModifierModul
return false; return false;
if (mouseX < this.x || mouseY < this.y || mouseX >= this.x + 140 || mouseY >= this.y + 12) if (mouseX < this.x || mouseY < this.y || mouseX >= this.x + 140 || mouseY >= this.y + 12)
return false; return false;
PacketButton.sendAndExecute(FilterModifierModuleGui.this.container.tile.getPos(), PacketButton.ButtonResult.TAG_FILTER, FilterModifierModuleGui.this.tags.indexOf(this.tag)); PacketButton.sendAndExecute(FilterModifierModuleGui.this.menu.tile.getBlockPos(), PacketButton.ButtonResult.TAG_FILTER, FilterModifierModuleGui.this.tags.indexOf(this.tag));
FilterModifierModuleGui.this.getMinecraft().getSoundHandler().play(SimpleSound.master(SoundEvents.UI_BUTTON_CLICK, 1)); FilterModifierModuleGui.this.getMinecraft().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1));
return true; return true;
} }
} }

View file

@ -4,19 +4,13 @@ import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem; import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.misc.ItemEquality; import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import joptsimple.internal.Strings; import joptsimple.internal.Strings;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.World; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable;
import java.util.List;
public class FilterModifierModuleItem extends ModuleItem { public class FilterModifierModuleItem extends ModuleItem {
@ -29,23 +23,18 @@ public class FilterModifierModuleItem extends ModuleItem {
} }
@Override @Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) { public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
return other != this; return other != this;
} }
@Override @Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) { public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
return this.type == ItemEquality.Type.TAG; return this.type == ItemEquality.Type.TAG;
} }
@Override @Override
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) { public AbstractPipeContainer<?> getContainer(ItemStack module, PipeBlockEntity tile, int windowId, Inventory inv, Player player, int moduleIndex) {
return new FilterModifierModuleContainer(Registry.filterModifierModuleContainer, windowId, player, tile.getPos(), moduleIndex); return new FilterModifierModuleContainer(Registry.filterModifierModuleContainer, windowId, player, tile.getBlockPos(), moduleIndex);
}
@Override
public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
super.addInformation(stack, worldIn, tooltip, flagIn);
} }
public ItemEquality getEqualityType(ItemStack stack) { public ItemEquality getEqualityType(ItemStack stack) {
@ -59,7 +48,7 @@ public class FilterModifierModuleItem extends ModuleItem {
public static ResourceLocation getFilterTag(ItemStack stack) { public static ResourceLocation getFilterTag(ItemStack stack) {
if (!stack.hasTag()) if (!stack.hasTag())
return null; return null;
String tag = stack.getTag().getString("filter_tag"); var tag = stack.getTag().getString("filter_tag");
if (Strings.isNullOrEmpty(tag)) if (Strings.isNullOrEmpty(tag))
return null; return null;
return new ResourceLocation(tag); return new ResourceLocation(tag);

View file

@ -3,10 +3,10 @@ package de.ellpeck.prettypipes.pipe.modules.retrieval;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer; import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.world.entity.player.Player;
import net.minecraft.inventory.container.Slot; import net.minecraft.world.inventory.MenuType;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.inventory.Slot;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -14,20 +14,20 @@ public class RetrievalModuleContainer extends AbstractPipeContainer<RetrievalMod
public ItemFilter filter; public ItemFilter filter;
public RetrievalModuleContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, BlockPos pos, int moduleIndex) { public RetrievalModuleContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos, int moduleIndex) {
super(type, id, player, pos, moduleIndex); super(type, id, player, pos, moduleIndex);
} }
@Override @Override
protected void addSlots() { protected void addSlots() {
this.filter = this.module.getItemFilter(this.moduleStack, this.tile); this.filter = this.module.getItemFilter(this.moduleStack, this.tile);
for (Slot slot : this.filter.getSlots((176 - this.module.filterSlots * 18) / 2 + 1, 17 + 32)) for (var slot : this.filter.getSlots((176 - this.module.filterSlots * 18) / 2 + 1, 17 + 32))
this.addSlot(slot); this.addSlot(slot);
} }
@Override @Override
public void onContainerClosed(PlayerEntity playerIn) { public void removed(Player playerIn) {
super.onContainerClosed(playerIn); super.removed(playerIn);
this.filter.save(); this.filter.save();
} }

View file

@ -1,19 +1,20 @@
package de.ellpeck.prettypipes.pipe.modules.retrieval; package de.ellpeck.prettypipes.pipe.modules.retrieval;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui;
import net.minecraft.client.gui.widget.Widget; import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.network.chat.Component;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Inventory;
public class RetrievalModuleGui extends AbstractPipeGui<RetrievalModuleContainer> { public class RetrievalModuleGui extends AbstractPipeGui<RetrievalModuleContainer> {
public RetrievalModuleGui(RetrievalModuleContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
public RetrievalModuleGui(RetrievalModuleContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
} }
@Override @Override
protected void init() { protected void init() {
super.init(); super.init();
for (Widget widget : this.container.filter.getButtons(this, this.guiLeft + 7, this.guiTop + 17 + 32 + 20)) for (var widget : this.menu.filter.getButtons(this, this.leftPos + 7, this.topPos + 17 + 32 + 20))
this.addButton(widget); this.addRenderableWidget(widget);
} }
} }

View file

@ -7,15 +7,16 @@ import de.ellpeck.prettypipes.items.ModuleTier;
import de.ellpeck.prettypipes.misc.ItemEquality; import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
public class RetrievalModuleItem extends ModuleItem { public class RetrievalModuleItem extends ModuleItem {
private final int maxExtraction; private final int maxExtraction;
private final int speed; private final int speed;
private final boolean preventOversending; private final boolean preventOversending;
@ -30,60 +31,60 @@ public class RetrievalModuleItem extends ModuleItem {
} }
@Override @Override
public void tick(ItemStack module, PipeTileEntity tile) { public void tick(ItemStack module, PipeBlockEntity tile) {
if (!tile.shouldWorkNow(this.speed) || !tile.canWork()) if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
return; return;
PipeNetwork network = PipeNetwork.get(tile.getWorld()); var network = PipeNetwork.get(tile.getLevel());
ItemEquality[] equalityTypes = ItemFilter.getEqualityTypes(tile); var equalityTypes = ItemFilter.getEqualityTypes(tile);
// loop through filters to see which items to pull // loop through filters to see which items to pull
for (ItemFilter subFilter : tile.getFilters()) { for (var subFilter : tile.getFilters()) {
for (int f = 0; f < subFilter.getSlots(); f++) { for (var f = 0; f < subFilter.getSlots(); f++) {
ItemStack filtered = subFilter.getStackInSlot(f); var filtered = subFilter.getStackInSlot(f);
if (filtered.isEmpty()) if (filtered.isEmpty())
continue; continue;
ItemStack copy = filtered.copy(); var copy = filtered.copy();
copy.setCount(this.maxExtraction); copy.setCount(this.maxExtraction);
Pair<BlockPos, ItemStack> dest = tile.getAvailableDestination(copy, true, this.preventOversending); var dest = tile.getAvailableDestination(copy, true, this.preventOversending);
if (dest == null) if (dest == null)
continue; continue;
ItemStack remain = dest.getRight().copy(); var remain = dest.getRight().copy();
// are we already waiting for crafting results? If so, don't request those again // are we already waiting for crafting results? If so, don't request those again
remain.shrink(network.getCurrentlyCraftingAmount(tile.getPos(), copy, equalityTypes)); remain.shrink(network.getCurrentlyCraftingAmount(tile.getBlockPos(), copy, equalityTypes));
if (network.requestItem(tile.getPos(), dest.getLeft(), remain, equalityTypes).isEmpty()) if (network.requestItem(tile.getBlockPos(), dest.getLeft(), remain, equalityTypes).isEmpty())
break; break;
} }
} }
} }
@Override @Override
public boolean canNetworkSee(ItemStack module, PipeTileEntity tile) { public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile) {
return false; return false;
} }
@Override @Override
public boolean canAcceptItem(ItemStack module, PipeTileEntity tile, ItemStack stack) { public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack) {
return false; return false;
} }
@Override @Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) { public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
return !(other instanceof RetrievalModuleItem); return !(other instanceof RetrievalModuleItem);
} }
@Override @Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) { public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
return true; return true;
} }
@Override @Override
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) { public AbstractPipeContainer<?> getContainer(ItemStack module, PipeBlockEntity tile, int windowId, Inventory inv, Player player, int moduleIndex) {
return new RetrievalModuleContainer(Registry.retrievalModuleContainer, windowId, player, tile.getPos(), moduleIndex); return new RetrievalModuleContainer(Registry.retrievalModuleContainer, windowId, player, tile.getBlockPos(), moduleIndex);
} }
@Override @Override
public ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile) { public ItemFilter getItemFilter(ItemStack module, PipeBlockEntity tile) {
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile); var filter = new ItemFilter(this.filterSlots, module, tile);
filter.canModifyWhitelist = false; filter.canModifyWhitelist = false;
filter.isWhitelist = true; filter.isWhitelist = true;
return filter; return filter;

View file

@ -1,14 +1,15 @@
package de.ellpeck.prettypipes.pipe.modules.stacksize; package de.ellpeck.prettypipes.pipe.modules.stacksize;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.world.entity.player.Player;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.inventory.MenuType;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class StackSizeModuleContainer extends AbstractPipeContainer<StackSizeModuleItem> { public class StackSizeModuleContainer extends AbstractPipeContainer<StackSizeModuleItem> {
public StackSizeModuleContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, BlockPos pos, int moduleIndex) {
public StackSizeModuleContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos, int moduleIndex) {
super(type, id, player, pos, moduleIndex); super(type, id, player, pos, moduleIndex);
} }

View file

@ -1,57 +1,59 @@
package de.ellpeck.prettypipes.pipe.modules.stacksize; package de.ellpeck.prettypipes.pipe.modules.stacksize;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.vertex.PoseStack;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.packets.PacketButton; import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketButton.ButtonResult; import de.ellpeck.prettypipes.packets.PacketButton.ButtonResult;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeGui;
import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.widget.button.Button; import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.resources.I18n; import net.minecraft.client.resources.language.I18n;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.network.chat.Component;
import net.minecraft.util.text.ITextComponent; import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.world.entity.player.Inventory;
import java.util.function.Supplier; import java.util.function.Supplier;
public class StackSizeModuleGui extends AbstractPipeGui<StackSizeModuleContainer> { public class StackSizeModuleGui extends AbstractPipeGui<StackSizeModuleContainer> {
public StackSizeModuleGui(StackSizeModuleContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
public StackSizeModuleGui(StackSizeModuleContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
} }
@Override @Override
protected void init() { protected void init() {
super.init(); super.init();
TextFieldWidget textField = this.addButton(new TextFieldWidget(this.font, this.guiLeft + 7, this.guiTop + 17 + 32 + 10, 40, 20, new TranslationTextComponent("info." + PrettyPipes.ID + ".max_stack_size")) { var textField = this.addRenderableWidget(new EditBox(this.font, this.leftPos + 7, this.topPos + 17 + 32 + 10, 40, 20, new TranslatableComponent("info." + PrettyPipes.ID + ".max_stack_size")) {
@Override @Override
public void writeText(String textToWrite) { public void insertText(String textToWrite) {
StringBuilder ret = new StringBuilder(); var ret = new StringBuilder();
for (char c : textToWrite.toCharArray()) { for (var c : textToWrite.toCharArray()) {
if (Character.isDigit(c)) if (Character.isDigit(c))
ret.append(c); ret.append(c);
} }
super.writeText(ret.toString()); super.insertText(ret.toString());
} }
}); });
textField.setText(String.valueOf(StackSizeModuleItem.getMaxStackSize(this.container.moduleStack))); textField.setValue(String.valueOf(StackSizeModuleItem.getMaxStackSize(this.menu.moduleStack)));
textField.setMaxStringLength(4); textField.setMaxLength(4);
textField.setResponder(s -> { textField.setResponder(s -> {
if (s.isEmpty()) if (s.isEmpty())
return; return;
int amount = Integer.parseInt(s); var amount = Integer.parseInt(s);
PacketButton.sendAndExecute(this.container.tile.getPos(), ButtonResult.STACK_SIZE_AMOUNT, amount); PacketButton.sendAndExecute(this.menu.tile.getBlockPos(), ButtonResult.STACK_SIZE_AMOUNT, amount);
}); });
Supplier<TranslationTextComponent> buttonText = () -> new TranslationTextComponent("info." + PrettyPipes.ID + ".limit_to_max_" + (StackSizeModuleItem.getLimitToMaxStackSize(this.container.moduleStack) ? "on" : "off")); var buttonText = (Supplier<TranslatableComponent>) () -> new TranslatableComponent("info." + PrettyPipes.ID + ".limit_to_max_" + (StackSizeModuleItem.getLimitToMaxStackSize(this.menu.moduleStack) ? "on" : "off"));
this.addButton(new Button(this.guiLeft + 7, this.guiTop + 17 + 32 + 10 + 22, 120, 20, buttonText.get(), b -> { this.addRenderableWidget(new Button(this.leftPos + 7, this.topPos + 17 + 32 + 10 + 22, 120, 20, buttonText.get(), b -> {
PacketButton.sendAndExecute(this.container.tile.getPos(), ButtonResult.STACK_SIZE_MODULE_BUTTON); PacketButton.sendAndExecute(this.menu.tile.getBlockPos(), ButtonResult.STACK_SIZE_MODULE_BUTTON);
b.setMessage(buttonText.get()); b.setMessage(buttonText.get());
})); }));
} }
@Override @Override
protected void drawGuiContainerForegroundLayer(MatrixStack matrix, int mouseX, int mouseY) { protected void renderLabels(PoseStack matrix, int mouseX, int mouseY) {
super.drawGuiContainerForegroundLayer(matrix, mouseX, mouseY); super.renderLabels(matrix, mouseX, mouseY);
this.font.drawString(matrix, I18n.format("info." + PrettyPipes.ID + ".max_stack_size") + ":", 7, 17 + 32, 4210752); this.font.draw(matrix, I18n.get("info." + PrettyPipes.ID + ".max_stack_size") + ":", 7, 17 + 32, 4210752);
} }
} }

View file

@ -4,10 +4,10 @@ import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem; import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.misc.ItemEquality; import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
@ -20,7 +20,7 @@ public class StackSizeModuleItem extends ModuleItem {
public static int getMaxStackSize(ItemStack module) { public static int getMaxStackSize(ItemStack module) {
if (module.hasTag()) { if (module.hasTag()) {
int amount = module.getTag().getInt("max_stack_size"); var amount = module.getTag().getInt("max_stack_size");
if (amount > 0) if (amount > 0)
return amount; return amount;
} }
@ -42,13 +42,13 @@ public class StackSizeModuleItem extends ModuleItem {
} }
@Override @Override
public int getMaxInsertionAmount(ItemStack module, PipeTileEntity tile, ItemStack stack, IItemHandler destination) { public int getMaxInsertionAmount(ItemStack module, PipeBlockEntity tile, ItemStack stack, IItemHandler destination) {
int max = getMaxStackSize(module); var max = getMaxStackSize(module);
if (getLimitToMaxStackSize(module)) if (getLimitToMaxStackSize(module))
max = Math.min(max, stack.getMaxStackSize()); max = Math.min(max, stack.getMaxStackSize());
int amount = 0; var amount = 0;
for (int i = 0; i < destination.getSlots(); i++) { for (var i = 0; i < destination.getSlots(); i++) {
ItemStack stored = destination.getStackInSlot(i); var stored = destination.getStackInSlot(i);
if (stored.isEmpty()) if (stored.isEmpty())
continue; continue;
if (!ItemEquality.compareItems(stored, stack)) if (!ItemEquality.compareItems(stored, stack))
@ -61,17 +61,17 @@ public class StackSizeModuleItem extends ModuleItem {
} }
@Override @Override
public boolean isCompatible(ItemStack module, PipeTileEntity tile, IModule other) { public boolean isCompatible(ItemStack module, PipeBlockEntity tile, IModule other) {
return !(other instanceof StackSizeModuleItem); return !(other instanceof StackSizeModuleItem);
} }
@Override @Override
public boolean hasContainer(ItemStack module, PipeTileEntity tile) { public boolean hasContainer(ItemStack module, PipeBlockEntity tile) {
return true; return true;
} }
@Override @Override
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) { public AbstractPipeContainer<?> getContainer(ItemStack module, PipeBlockEntity tile, int windowId, Inventory inv, Player player, int moduleIndex) {
return new StackSizeModuleContainer(Registry.stackSizeModuleContainer, windowId, player, tile.getPos(), moduleIndex); return new StackSizeModuleContainer(Registry.stackSizeModuleContainer, windowId, player, tile.getBlockPos(), moduleIndex);
} }
} }

View file

@ -1,56 +1,68 @@
package de.ellpeck.prettypipes.pressurizer; package de.ellpeck.prettypipes.pressurizer;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import net.minecraft.block.BlockRenderType; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.block.BlockState; import net.minecraft.core.BlockPos;
import net.minecraft.block.ContainerBlock; import net.minecraft.network.chat.Component;
import net.minecraft.block.SoundType; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.block.material.Material; import net.minecraft.world.InteractionHand;
import net.minecraft.client.util.ITooltipFlag; import net.minecraft.world.InteractionResult;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.world.entity.player.Player;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.world.item.TooltipFlag;
import net.minecraft.util.ActionResultType; import net.minecraft.world.level.BlockGetter;
import net.minecraft.util.Direction; import net.minecraft.world.level.Level;
import net.minecraft.util.Hand; import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.level.block.RenderShape;
import net.minecraft.util.math.BlockRayTraceResult; import net.minecraft.world.level.block.SoundType;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.IBlockReader; import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.World; import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.fml.network.NetworkHooks; import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.network.NetworkHooks;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.List; import java.util.List;
public class PressurizerBlock extends ContainerBlock { public class PressurizerBlock extends BaseEntityBlock {
public PressurizerBlock() { public PressurizerBlock() {
super(Properties.create(Material.ROCK).hardnessAndResistance(3).sound(SoundType.STONE)); super(BlockBehaviour.Properties.of(Material.STONE).strength(3).sound(SoundType.STONE));
} }
@Override @Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult result) { public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult result) {
PressurizerBlockEntity tile = Utility.getBlockEntity(PressurizerBlockEntity.class, worldIn, pos); var tile = Utility.getBlockEntity(PressurizerBlockEntity.class, worldIn, pos);
if (tile == null) if (tile == null)
return ActionResultType.PASS; return InteractionResult.PASS;
if (!worldIn.isRemote) if (!worldIn.isClientSide)
NetworkHooks.openGui((ServerPlayerEntity) player, tile, pos); NetworkHooks.openGui((ServerPlayer) player, tile, pos);
return ActionResultType.SUCCESS; return InteractionResult.SUCCESS;
}
@org.jetbrains.annotations.Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new PressurizerBlockEntity(pos, state);
} }
@Override @Override
public TileEntity createNewTileEntity(IBlockReader worldIn) { public RenderShape getRenderShape(BlockState state) {
return new PressurizerBlockEntity(); return RenderShape.MODEL;
} }
@Override @Override
public BlockRenderType getRenderType(BlockState state) { public void appendHoverText(ItemStack stack, @Nullable BlockGetter worldIn, List<Component> tooltip, TooltipFlag flagIn) {
return BlockRenderType.MODEL;
}
@Override
public void addInformation(ItemStack stack, @Nullable IBlockReader worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
Utility.addTooltip(this.getRegistryName().getPath(), tooltip); Utility.addTooltip(this.getRegistryName().getPath(), tooltip);
} }
@org.jetbrains.annotations.Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
return level.isClientSide ? null : createTickerHelper(type, Registry.pressurizerBlockEntity, PressurizerBlockEntity::tick);
}
} }

View file

@ -6,25 +6,21 @@ import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.ConnectionType; import de.ellpeck.prettypipes.pipe.ConnectionType;
import de.ellpeck.prettypipes.pipe.IPipeConnectable; import de.ellpeck.prettypipes.pipe.IPipeConnectable;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import net.minecraft.core.BlockPos;
import net.minecraft.block.BlockState; import net.minecraft.core.Direction;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.NetworkManager; import net.minecraft.network.Connection;
import net.minecraft.network.play.server.SUpdateTileEntityPacket; import net.minecraft.network.chat.Component;
import net.minecraft.tileentity.ITickableTileEntity; import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.tileentity.TileEntity; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.tileentity.TileEntityType; import net.minecraft.world.MenuProvider;
import net.minecraft.util.Direction; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.entity.player.Player;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.World; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.CapabilityEnergy; import net.minecraftforge.energy.CapabilityEnergy;
@ -33,19 +29,19 @@ import net.minecraftforge.energy.IEnergyStorage;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class PressurizerBlockEntity extends BlockEntity implements INamedContainerProvider, ITickableTileEntity, IPipeConnectable { public class PressurizerBlockEntity extends BlockEntity implements MenuProvider, IPipeConnectable {
private final ModifiableEnergyStorage storage = new ModifiableEnergyStorage(64000, 512, 0); private final ModifiableEnergyStorage storage = new ModifiableEnergyStorage(64000, 512, 0);
private final LazyOptional<IEnergyStorage> lazyStorage = LazyOptional.of(() -> this.storage); private final LazyOptional<IEnergyStorage> lazyStorage = LazyOptional.of(() -> this.storage);
private final LazyOptional<IPipeConnectable> lazyThis = LazyOptional.of(() -> this); private final LazyOptional<IPipeConnectable> lazyThis = LazyOptional.of(() -> this);
private int lastEnergy; private int lastEnergy;
public PressurizerBlockEntity() { public PressurizerBlockEntity(BlockPos pos, BlockState state) {
super(Registry.pressurizerTileEntity); super(Registry.pressurizerBlockEntity, pos, state);
} }
public boolean pressurizeItem(ItemStack stack, boolean simulate) { public boolean pressurizeItem(ItemStack stack, boolean simulate) {
int amount = 100 * stack.getCount(); var amount = 100 * stack.getCount();
return this.storage.extractInternal(amount, simulate) >= amount; return this.storage.extractInternal(amount, simulate) >= amount;
} }
@ -62,41 +58,41 @@ public class PressurizerBlockEntity extends BlockEntity implements INamedContain
} }
@Override @Override
public CompoundTag write(CompoundTag compound) { public CompoundTag save(CompoundTag compound) {
compound.putInt("energy", this.getEnergy()); compound.putInt("energy", this.getEnergy());
return super.write(compound); return super.save(compound);
} }
@Override @Override
public void read(BlockState state, CompoundTag nbt) { public void load(CompoundTag nbt) {
this.storage.setEnergyStored(nbt.getInt("energy")); this.storage.setEnergyStored(nbt.getInt("energy"));
super.read(state, nbt); super.load(nbt);
} }
@Override @Override
public CompoundTag getUpdateTag() { public CompoundTag getUpdateTag() {
return this.write(new CompoundTag()); return this.save(new CompoundTag());
} }
@Override @Override
public void handleUpdateTag(BlockState state, CompoundTag tag) { public void handleUpdateTag(CompoundTag tag) {
this.read(state, tag); this.load(tag);
} }
@Override @Override
public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) { public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) {
this.read(this.getBlockState(), pkt.getNbtCompound()); this.load(pkt.getTag());
} }
@Override @Override
public ITextComponent getDisplayName() { public Component getDisplayName() {
return new TranslationTextComponent("container." + PrettyPipes.ID + ".pressurizer"); return new TranslatableComponent("container." + PrettyPipes.ID + ".pressurizer");
} }
@Nullable @Nullable
@Override @Override
public Container createMenu(int window, PlayerInventory inv, PlayerEntity player) { public AbstractContainerMenu createMenu(int window, Inventory inv, Player player) {
return new PressurizerContainer(Registry.pressurizerContainer, window, player, this.pos); return new PressurizerContainer(Registry.pressurizerContainer, window, player, this.worldPosition);
} }
@Override @Override
@ -111,43 +107,40 @@ public class PressurizerBlockEntity extends BlockEntity implements INamedContain
} }
@Override @Override
public void remove() { public void setRemoved() {
super.remove(); super.setRemoved();
this.lazyStorage.invalidate(); this.lazyStorage.invalidate();
this.lazyThis.invalidate(); this.lazyThis.invalidate();
} }
@Override @Override
public void tick() { public ConnectionType getConnectionType(BlockPos pipePos, Direction direction) {
if (this.world.isRemote) return ConnectionType.CONNECTED;
return; }
public static void tick(Level level, BlockPos pos, BlockState state, PressurizerBlockEntity pressurizer) {
// notify pipes in network about us // notify pipes in network about us
if (this.world.getGameTime() % 10 == 0) { if (pressurizer.level.getGameTime() % 10 == 0) {
PipeNetwork network = PipeNetwork.get(this.world); var network = PipeNetwork.get(pressurizer.level);
for (Direction dir : Direction.values()) { for (var dir : Direction.values()) {
BlockPos offset = this.pos.offset(dir); var offset = pressurizer.worldPosition.relative(dir);
for (BlockPos node : network.getOrderedNetworkNodes(offset)) { for (var node : network.getOrderedNetworkNodes(offset)) {
if (!this.world.isBlockLoaded(node)) if (!pressurizer.level.isLoaded(node))
continue; continue;
PipeTileEntity pipe = network.getPipe(node); var pipe = network.getPipe(node);
if (pipe != null) if (pipe != null)
pipe.pressurizer = this; pipe.pressurizer = pressurizer;
} }
} }
} }
// send energy update // send energy update
if (this.lastEnergy != this.storage.getEnergyStored() && this.world.getGameTime() % 10 == 0) { if (pressurizer.lastEnergy != pressurizer.storage.getEnergyStored() && pressurizer.level.getGameTime() % 10 == 0) {
this.lastEnergy = this.storage.getEnergyStored(); pressurizer.lastEnergy = pressurizer.storage.getEnergyStored();
Utility.sendBlockEntityToClients(this); Utility.sendBlockEntityToClients(pressurizer);
} }
} }
@Override
public ConnectionType getConnectionType(BlockPos pipePos, Direction direction) {
return ConnectionType.CONNECTED;
}
private static class ModifiableEnergyStorage extends EnergyStorage { private static class ModifiableEnergyStorage extends EnergyStorage {
public ModifiableEnergyStorage(int capacity, int maxReceive, int maxExtract) { public ModifiableEnergyStorage(int capacity, int maxReceive, int maxExtract) {
@ -159,7 +152,7 @@ public class PressurizerBlockEntity extends BlockEntity implements INamedContain
} }
private int extractInternal(int maxExtract, boolean simulate) { private int extractInternal(int maxExtract, boolean simulate) {
int energyExtracted = Math.min(this.energy, maxExtract); var energyExtracted = Math.min(this.energy, maxExtract);
if (!simulate) if (!simulate)
this.energy -= energyExtracted; this.energy -= energyExtracted;
return energyExtracted; return energyExtracted;

View file

@ -2,40 +2,36 @@ package de.ellpeck.prettypipes.pressurizer;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.inventory.container.Slot;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType; import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot; import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class PressurizerContainer extends AbstractContainerMenu { public class PressurizerContainer extends AbstractContainerMenu {
public final PressurizerBlockEntity tile; public final PressurizerBlockEntity tile;
public PressurizerContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos) { public PressurizerContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos) {
super(type, id); super(type, id);
this.tile = Utility.getBlockEntity(PressurizerBlockEntity.class, player.level, pos); this.tile = Utility.getBlockEntity(PressurizerBlockEntity.class, player.level, pos);
for (int l = 0; l < 3; ++l) for (var l = 0; l < 3; ++l)
for (int j1 = 0; j1 < 9; ++j1) for (var j1 = 0; j1 < 9; ++j1)
this.addSlot(new Slot(player.getInventory(), j1 + l * 9 + 9, 8 + j1 * 18, 55 + l * 18)); this.addSlot(new Slot(player.getInventory(), j1 + l * 9 + 9, 8 + j1 * 18, 55 + l * 18));
for (int i1 = 0; i1 < 9; ++i1) for (var i1 = 0; i1 < 9; ++i1)
this.addSlot(new Slot(player.getInventory(), i1, 8 + i1 * 18, 113)); this.addSlot(new Slot(player.getInventory(), i1, 8 + i1 * 18, 113));
} }
@Override @Override
public ItemStack transferStackInSlot(PlayerEntity player, int slotIndex) { public ItemStack quickMoveStack(Player player, int slotIndex) {
return Utility.transferStackInSlot(this, this::mergeItemStack, player, slotIndex, stack -> null); return Utility.transferStackInSlot(this, this::moveItemStackTo, player, slotIndex, stack -> null);
} }
@Override @Override
public boolean canInteractWith(PlayerEntity playerIn) { public boolean stillValid(Player player) {
return true; return true;
} }
} }

View file

@ -1,43 +1,43 @@
package de.ellpeck.prettypipes.pressurizer; package de.ellpeck.prettypipes.pressurizer;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.vertex.PoseStack;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import net.minecraft.client.gui.screen.inventory.ContainerScreen; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.widget.Widget; import net.minecraft.network.chat.Component;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.util.text.TranslationTextComponent;
public class PressurizerGui extends AbstractContainerScreen<PressurizerContainer> {
public class PressurizerGui extends ContainerScreen<PressurizerContainer> {
private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/pressurizer.png"); private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/pressurizer.png");
public PressurizerGui(PressurizerContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) { public PressurizerGui(PressurizerContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
this.xSize = 176; this.imageWidth = 176;
this.ySize = 137; this.imageHeight = 137;
} }
@Override @Override
public void render(MatrixStack matrix, int mouseX, int mouseY, float partialTicks) { public void render(PoseStack matrix, int mouseX, int mouseY, float partialTicks) {
this.renderBackground(matrix); this.renderBackground(matrix);
super.render(matrix, mouseX, mouseY, partialTicks); super.render(matrix, mouseX, mouseY, partialTicks);
this.func_230459_a_(matrix, mouseX, mouseY); this.renderTooltip(matrix, mouseX, mouseY);
if (mouseX >= this.guiLeft + 26 && mouseY >= this.guiTop + 22 && mouseX < this.guiLeft + 26 + 124 && mouseY < this.guiTop + 22 + 12) if (mouseX >= this.leftPos + 26 && mouseY >= this.topPos + 22 && mouseX < this.leftPos + 26 + 124 && mouseY < this.topPos + 22 + 12)
this.renderTooltip(matrix, new TranslationTextComponent("info." + PrettyPipes.ID + ".energy", this.container.tile.getEnergy(), this.container.tile.getMaxEnergy()), mouseX, mouseY); this.renderTooltip(matrix, new TranslatableComponent("info." + PrettyPipes.ID + ".energy", this.menu.tile.getEnergy(), this.menu.tile.getMaxEnergy()), mouseX, mouseY);
} }
@Override @Override
protected void drawGuiContainerForegroundLayer(MatrixStack matrix, int mouseX, int mouseY) { protected void renderLabels(PoseStack matrix, int mouseX, int mouseY) {
this.font.drawString(matrix, this.playerInventory.getDisplayName().getString(), 8, this.ySize - 96 + 2, 4210752); this.font.draw(matrix, this.playerInventoryTitle.getString(), 8, this.imageHeight - 96 + 2, 4210752);
this.font.drawString(matrix, this.title.getString(), 8, 6, 4210752); this.font.draw(matrix, this.title.getString(), 8, 6, 4210752);
} }
@Override @Override
protected void drawGuiContainerBackgroundLayer(MatrixStack matrixStack, float partialTicks, int x, int y) { protected void renderBg(PoseStack matrixStack, float partialTicks, int x, int y) {
this.getMinecraft().getTextureManager().bindTexture(TEXTURE); this.getMinecraft().getTextureManager().bindForSetup(TEXTURE);
this.blit(matrixStack, this.guiLeft, this.guiTop, 0, 0, 176, 137); this.blit(matrixStack, this.leftPos, this.topPos, 0, 0, 176, 137);
int energy = (int) (this.container.tile.getEnergyPercentage() * 124); var energy = (int) (this.menu.tile.getEnergyPercentage() * 124);
this.blit(matrixStack, this.guiLeft + 26, this.guiTop + 22, 0, 137, energy, 12); this.blit(matrixStack, this.leftPos + 26, this.topPos + 22, 0, 137, energy, 12);
} }
} }

View file

@ -1,16 +1,23 @@
package de.ellpeck.prettypipes.terminal; package de.ellpeck.prettypipes.terminal;
import net.minecraft.tileentity.TileEntity; import de.ellpeck.prettypipes.Registry;
import net.minecraft.world.IBlockReader; import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import javax.annotation.Nullable; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
public class CraftingTerminalBlock extends ItemTerminalBlock { public class CraftingTerminalBlock extends ItemTerminalBlock {
@Nullable
@Override @Override
public TileEntity createNewTileEntity(IBlockReader worldIn) { public @org.jetbrains.annotations.Nullable BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new CraftingTerminalTileEntity(); return new CraftingTerminalBlockEntity(pos, state);
} }
@org.jetbrains.annotations.Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
return createTickerHelper(type, Registry.craftingTerminalBlockEntity, ItemTerminalBlockEntity::tick);
}
} }

View file

@ -7,25 +7,24 @@ import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.misc.EquatableItemStack; import de.ellpeck.prettypipes.misc.EquatableItemStack;
import de.ellpeck.prettypipes.misc.ItemEquality; import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.network.NetworkItem;
import de.ellpeck.prettypipes.network.NetworkLocation; import de.ellpeck.prettypipes.network.NetworkLocation;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.packets.PacketGhostSlot; import de.ellpeck.prettypipes.packets.PacketGhostSlot;
import de.ellpeck.prettypipes.packets.PacketHandler; import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer; import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer;
import net.minecraft.block.BlockState; import net.minecraft.ChatFormatting;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.core.Direction;
import net.minecraft.inventory.container.Container;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Direction; import net.minecraft.network.chat.Component;
import net.minecraft.util.math.BlockPos; import net.minecraft.network.chat.Style;
import net.minecraft.util.text.ITextComponent; import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.util.text.Style; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.util.text.TextFormatting; import net.minecraft.world.entity.player.Player;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler; import net.minecraftforge.items.ItemStackHandler;
import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.mutable.MutableInt;
@ -36,23 +35,23 @@ import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
public class CraftingTerminalTileEntity extends ItemTerminalTileEntity { public class CraftingTerminalBlockEntity extends ItemTerminalBlockEntity {
public final ItemStackHandler craftItems = new ItemStackHandler(9) { public final ItemStackHandler craftItems = new ItemStackHandler(9) {
@Override @Override
protected void onContentsChanged(int slot) { protected void onContentsChanged(int slot) {
for (PlayerEntity playerEntity : CraftingTerminalTileEntity.this.getLookingPlayers()) for (var playerEntity : CraftingTerminalBlockEntity.this.getLookingPlayers())
playerEntity.openContainer.onCraftMatrixChanged(null); playerEntity.containerMenu.slotsChanged(null);
} }
}; };
public final ItemStackHandler ghostItems = new ItemStackHandler(9); public final ItemStackHandler ghostItems = new ItemStackHandler(9);
public CraftingTerminalTileEntity() { public CraftingTerminalBlockEntity(BlockPos pos, BlockState state) {
super(Registry.craftingTerminalTileEntity); super(Registry.craftingTerminalBlockEntity, pos, state);
} }
public ItemStack getRequestedCraftItem(int slot) { public ItemStack getRequestedCraftItem(int slot) {
ItemStack stack = this.craftItems.getStackInSlot(slot); var stack = this.craftItems.getStackInSlot(slot);
if (!stack.isEmpty()) if (!stack.isEmpty())
return stack; return stack;
return this.ghostItems.getStackInSlot(slot); return this.ghostItems.getStackInSlot(slot);
@ -64,30 +63,30 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
public void setGhostItems(ListMultimap<Integer, ItemStack> stacks) { public void setGhostItems(ListMultimap<Integer, ItemStack> stacks) {
this.updateItems(); this.updateItems();
for (int i = 0; i < this.ghostItems.getSlots(); i++) { for (var i = 0; i < this.ghostItems.getSlots(); i++) {
List<ItemStack> items = stacks.get(i); var items = stacks.get(i);
if (items.isEmpty()) { if (items.isEmpty()) {
this.ghostItems.setStackInSlot(i, ItemStack.EMPTY); this.ghostItems.setStackInSlot(i, ItemStack.EMPTY);
continue; continue;
} }
ItemStack toSet = items.get(0); var toSet = items.get(0);
// if we have more than one item to choose from, we want to pick the one that we have most of in the system // if we have more than one item to choose from, we want to pick the one that we have most of in the system
if (items.size() > 1) { if (items.size() > 1) {
int highestAmount = 0; var highestAmount = 0;
for (ItemStack stack : items) { for (var stack : items) {
int amount = 0; var amount = 0;
// check existing items // check existing items
NetworkItem network = this.networkItems.get(new EquatableItemStack(stack, ItemEquality.NBT)); var network = this.networkItems.get(new EquatableItemStack(stack, ItemEquality.NBT));
if (network != null) { if (network != null) {
amount = network.getLocations().stream() amount = network.getLocations().stream()
.mapToInt(l -> l.getItemAmount(this.world, stack, ItemEquality.NBT)) .mapToInt(l -> l.getItemAmount(this.level, stack, ItemEquality.NBT))
.sum(); .sum();
} }
// check craftables // check craftables
if (amount <= 0 && highestAmount <= 0) { if (amount <= 0 && highestAmount <= 0) {
PipeTileEntity pipe = this.getConnectedPipe(); var pipe = this.getConnectedPipe();
if (pipe != null) if (pipe != null)
amount = PipeNetwork.get(this.world).getCraftableAmount(pipe.getPos(), null, stack, new Stack<>(), ItemEquality.NBT); amount = PipeNetwork.get(this.level).getCraftableAmount(pipe.getBlockPos(), null, stack, new Stack<>(), ItemEquality.NBT);
} }
if (amount > highestAmount) { if (amount > highestAmount) {
highestAmount = amount; highestAmount = amount;
@ -98,77 +97,77 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
this.ghostItems.setStackInSlot(i, toSet.copy()); this.ghostItems.setStackInSlot(i, toSet.copy());
} }
if (!this.world.isRemote) { if (!this.level.isClientSide) {
ListMultimap<Integer, ItemStack> clients = ArrayListMultimap.create(); ListMultimap<Integer, ItemStack> clients = ArrayListMultimap.create();
for (int i = 0; i < this.ghostItems.getSlots(); i++) for (var i = 0; i < this.ghostItems.getSlots(); i++)
clients.put(i, this.ghostItems.getStackInSlot(i)); clients.put(i, this.ghostItems.getStackInSlot(i));
PacketHandler.sendToAllLoaded(this.world, this.pos, new PacketGhostSlot(this.pos, clients)); PacketHandler.sendToAllLoaded(this.level, this.getBlockPos(), new PacketGhostSlot(this.getBlockPos(), clients));
} }
} }
public void requestCraftingItems(PlayerEntity player, int maxAmount) { public void requestCraftingItems(Player player, int maxAmount) {
PipeTileEntity pipe = this.getConnectedPipe(); var pipe = this.getConnectedPipe();
if (pipe == null) if (pipe == null)
return; return;
PipeNetwork network = PipeNetwork.get(this.world); var network = PipeNetwork.get(this.level);
network.startProfile("terminal_request_crafting"); network.startProfile("terminal_request_crafting");
this.updateItems(); this.updateItems();
// get the amount of crafts that we can do // get the amount of crafts that we can do
int lowestAvailable = getAvailableCrafts(pipe, this.craftItems.getSlots(), i -> ItemHandlerHelper.copyStackWithSize(this.getRequestedCraftItem(i), 1), this::isGhostItem, s -> { var lowestAvailable = getAvailableCrafts(pipe, this.craftItems.getSlots(), i -> ItemHandlerHelper.copyStackWithSize(this.getRequestedCraftItem(i), 1), this::isGhostItem, s -> {
NetworkItem item = this.networkItems.get(s); var item = this.networkItems.get(s);
return item != null ? item.getLocations() : Collections.emptyList(); return item != null ? item.getLocations() : Collections.emptyList();
}, onItemUnavailable(player), new Stack<>(), ItemEquality.NBT); }, onItemUnavailable(player), new Stack<>(), ItemEquality.NBT);
if (lowestAvailable > 0) { if (lowestAvailable > 0) {
// if we're limiting the amount, pretend we only have that amount available // if we're limiting the amount, pretend we only have that amount available
if (maxAmount < lowestAvailable) if (maxAmount < lowestAvailable)
lowestAvailable = maxAmount; lowestAvailable = maxAmount;
for (int i = 0; i < this.craftItems.getSlots(); i++) { for (var i = 0; i < this.craftItems.getSlots(); i++) {
ItemStack requested = this.getRequestedCraftItem(i); var requested = this.getRequestedCraftItem(i);
if (requested.isEmpty()) if (requested.isEmpty())
continue; continue;
requested = requested.copy(); requested = requested.copy();
requested.setCount(lowestAvailable); requested.setCount(lowestAvailable);
this.requestItemImpl(requested, onItemUnavailable(player)); this.requestItemImpl(requested, onItemUnavailable(player));
} }
player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".sending_ingredients", lowestAvailable).setStyle(Style.EMPTY.setFormatting(TextFormatting.GREEN)), UUID.randomUUID()); player.sendMessage(new TranslatableComponent("info." + PrettyPipes.ID + ".sending_ingredients", lowestAvailable).setStyle(Style.EMPTY.applyFormat(ChatFormatting.GREEN)), UUID.randomUUID());
} }
network.endProfile(); network.endProfile();
} }
@Override @Override
public CompoundTag write(CompoundTag compound) { public CompoundTag save(CompoundTag compound) {
compound.put("craft_items", this.craftItems.serializeNBT()); compound.put("craft_items", this.craftItems.serializeNBT());
return super.write(compound); return super.save(compound);
} }
@Override @Override
public void read(BlockState state, CompoundTag compound) { public void load(CompoundTag compound) {
this.craftItems.deserializeNBT(compound.getCompound("craft_items")); this.craftItems.deserializeNBT(compound.getCompound("craft_items"));
super.read(state, compound); super.load(compound);
} }
@Override @Override
public ITextComponent getDisplayName() { public Component getDisplayName() {
return new TranslationTextComponent("container." + PrettyPipes.ID + ".crafting_terminal"); return new TranslatableComponent("container." + PrettyPipes.ID + ".crafting_terminal");
} }
@Nullable @Nullable
@Override @Override
public Container createMenu(int window, PlayerInventory inv, PlayerEntity player) { public AbstractContainerMenu createMenu(int window, Inventory inv, Player player) {
return new CraftingTerminalContainer(Registry.craftingTerminalContainer, window, player, this.pos); return new CraftingTerminalContainer(Registry.craftingTerminalContainer, window, player, this.worldPosition);
} }
@Override @Override
public ItemStack insertItem(BlockPos pipePos, Direction direction, ItemStack remain, boolean simulate) { public ItemStack insertItem(BlockPos pipePos, Direction direction, ItemStack remain, boolean simulate) {
BlockPos pos = pipePos.offset(direction); var pos = pipePos.relative(direction);
CraftingTerminalTileEntity tile = Utility.getBlockEntity(CraftingTerminalTileEntity.class, this.world, pos); var tile = Utility.getBlockEntity(CraftingTerminalBlockEntity.class, this.level, pos);
if (tile != null) { if (tile != null) {
remain = remain.copy(); remain = remain.copy();
int lowestSlot = -1; var lowestSlot = -1;
do { do {
for (int i = 0; i < tile.craftItems.getSlots(); i++) { for (var i = 0; i < tile.craftItems.getSlots(); i++) {
ItemStack stack = tile.getRequestedCraftItem(i); var stack = tile.getRequestedCraftItem(i);
int count = tile.isGhostItem(i) ? 0 : stack.getCount(); var count = tile.isGhostItem(i) ? 0 : stack.getCount();
if (!ItemHandlerHelper.canItemStacksStack(stack, remain)) if (!ItemHandlerHelper.canItemStacksStack(stack, remain))
continue; continue;
// ensure that a single non-stackable item can still enter a ghost slot // ensure that a single non-stackable item can still enter a ghost slot
@ -178,7 +177,7 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
lowestSlot = i; lowestSlot = i;
} }
if (lowestSlot >= 0) { if (lowestSlot >= 0) {
ItemStack copy = remain.copy(); var copy = remain.copy();
copy.setCount(1); copy.setCount(1);
// if there were remaining items inserting into the slot with lowest contents, we're overflowing // if there were remaining items inserting into the slot with lowest contents, we're overflowing
if (tile.craftItems.insertItem(lowestSlot, copy, simulate).getCount() > 0) if (tile.craftItems.insertItem(lowestSlot, copy, simulate).getCount() > 0)
@ -194,33 +193,33 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
return remain; return remain;
} }
public static int getAvailableCrafts(PipeTileEntity tile, int slots, Function<Integer, ItemStack> inputFunction, Predicate<Integer> isGhost, Function<EquatableItemStack, Collection<NetworkLocation>> locationsFunction, Consumer<ItemStack> unavailableConsumer, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) { public static int getAvailableCrafts(PipeBlockEntity tile, int slots, Function<Integer, ItemStack> inputFunction, Predicate<Integer> isGhost, Function<EquatableItemStack, Collection<NetworkLocation>> locationsFunction, Consumer<ItemStack> unavailableConsumer, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) {
PipeNetwork network = PipeNetwork.get(tile.getWorld()); var network = PipeNetwork.get(tile.getLevel());
// the highest amount we can craft with the items we have // the highest amount we can craft with the items we have
int lowestAvailable = Integer.MAX_VALUE; var lowestAvailable = Integer.MAX_VALUE;
// this is the amount of items required for each ingredient when crafting ONE // this is the amount of items required for each ingredient when crafting ONE
Map<EquatableItemStack, MutableInt> requiredItems = new HashMap<>(); Map<EquatableItemStack, MutableInt> requiredItems = new HashMap<>();
for (int i = 0; i < slots; i++) { for (var i = 0; i < slots; i++) {
ItemStack requested = inputFunction.apply(i); var requested = inputFunction.apply(i);
if (requested.isEmpty()) if (requested.isEmpty())
continue; continue;
MutableInt amount = requiredItems.computeIfAbsent(new EquatableItemStack(requested, equalityTypes), s -> new MutableInt()); var amount = requiredItems.computeIfAbsent(new EquatableItemStack(requested, equalityTypes), s -> new MutableInt());
amount.add(requested.getCount()); amount.add(requested.getCount());
// if no items fit into the crafting input, we still want to pretend they do for requesting // if no items fit into the crafting input, we still want to pretend they do for requesting
int fit = Math.max(requested.getMaxStackSize() - (isGhost.test(i) ? 0 : requested.getCount()), 1); var fit = Math.max(requested.getMaxStackSize() - (isGhost.test(i) ? 0 : requested.getCount()), 1);
if (lowestAvailable > fit) if (lowestAvailable > fit)
lowestAvailable = fit; lowestAvailable = fit;
} }
for (Map.Entry<EquatableItemStack, MutableInt> entry : requiredItems.entrySet()) { for (var entry : requiredItems.entrySet()) {
EquatableItemStack stack = entry.getKey(); var stack = entry.getKey();
// total amount of available items of this type // total amount of available items of this type
int available = 0; var available = 0;
for (NetworkLocation location : locationsFunction.apply(stack)) { for (var location : locationsFunction.apply(stack)) {
int amount = location.getItemAmount(tile.getWorld(), stack.stack, equalityTypes); var amount = location.getItemAmount(tile.getLevel(), stack.stack(), equalityTypes);
if (amount <= 0) if (amount <= 0)
continue; continue;
amount -= network.getLockedAmount(location.getPos(), stack.stack, null, equalityTypes); amount -= network.getLockedAmount(location.getPos(), stack.stack(), null, equalityTypes);
available += amount; available += amount;
} }
// divide the total by the amount required to get the amount that // divide the total by the amount required to get the amount that
@ -229,17 +228,17 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
// check how many craftable items we have and add those on if we need to // check how many craftable items we have and add those on if we need to
if (available < lowestAvailable) { if (available < lowestAvailable) {
int craftable = network.getCraftableAmount(tile.getPos(), unavailableConsumer, stack.stack, dependencyChain, equalityTypes); var craftable = network.getCraftableAmount(tile.getBlockPos(), unavailableConsumer, stack.stack(), dependencyChain, equalityTypes);
if (craftable > 0) if (craftable > 0)
available += craftable / entry.getValue().intValue(); available += craftable / entry.getValue().intValue();
} }
// clamp to lowest available // clamp to the lowest available
if (available < lowestAvailable) if (available < lowestAvailable)
lowestAvailable = available; lowestAvailable = available;
if (available <= 0 && unavailableConsumer != null) if (available <= 0 && unavailableConsumer != null)
unavailableConsumer.accept(stack.stack); unavailableConsumer.accept(stack.stack());
} }
return lowestAvailable; return lowestAvailable;
} }

View file

@ -1,79 +1,88 @@
package de.ellpeck.prettypipes.terminal; package de.ellpeck.prettypipes.terminal;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import net.minecraft.block.BlockRenderType; import net.minecraft.ChatFormatting;
import net.minecraft.block.BlockState; import net.minecraft.core.BlockPos;
import net.minecraft.block.ContainerBlock; import net.minecraft.network.chat.Component;
import net.minecraft.block.SoundType; import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.block.material.Material; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.client.util.ITooltipFlag; import net.minecraft.world.InteractionHand;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.world.InteractionResult;
import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.world.item.TooltipFlag;
import net.minecraft.util.ActionResultType; import net.minecraft.world.level.BlockGetter;
import net.minecraft.util.Direction; import net.minecraft.world.level.Level;
import net.minecraft.util.Hand; import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.level.block.RenderShape;
import net.minecraft.util.math.BlockRayTraceResult; import net.minecraft.world.level.block.SoundType;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.util.text.TextFormatting; import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.IBlockReader; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.World; import net.minecraft.world.level.material.Material;
import net.minecraftforge.fml.network.NetworkHooks; import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.network.NetworkHooks;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public class ItemTerminalBlock extends ContainerBlock { public class ItemTerminalBlock extends BaseEntityBlock {
public ItemTerminalBlock() { public ItemTerminalBlock() {
super(Properties.create(Material.ROCK).hardnessAndResistance(3).sound(SoundType.STONE)); super(Properties.of(Material.STONE).strength(3).sound(SoundType.STONE));
} }
@Override @Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult result) { public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult result) {
ItemTerminalTileEntity tile = Utility.getBlockEntity(ItemTerminalTileEntity.class, worldIn, pos); var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, worldIn, pos);
if (tile == null) if (tile == null)
return ActionResultType.PASS; return InteractionResult.PASS;
String reason = tile.getInvalidTerminalReason(); var reason = tile.getInvalidTerminalReason();
if (reason != null) { if (reason != null) {
if (!worldIn.isRemote) if (!worldIn.isClientSide)
player.sendMessage(new TranslationTextComponent(reason).mergeStyle(TextFormatting.RED), UUID.randomUUID()); player.sendMessage(new TranslatableComponent(reason).withStyle(ChatFormatting.RED), UUID.randomUUID());
return ActionResultType.SUCCESS; return InteractionResult.SUCCESS;
} }
if (!worldIn.isRemote) { if (!worldIn.isClientSide) {
NetworkHooks.openGui((ServerPlayerEntity) player, tile, pos); NetworkHooks.openGui((ServerPlayer) player, tile, pos);
tile.updateItems(player); tile.updateItems(player);
} }
return ActionResultType.SUCCESS; return InteractionResult.SUCCESS;
} }
@Override @Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) { public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
if (state.getBlock() != newState.getBlock()) { if (state.getBlock() != newState.getBlock()) {
ItemTerminalTileEntity tile = Utility.getBlockEntity(ItemTerminalTileEntity.class, worldIn, pos); var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, worldIn, pos);
if (tile != null) if (tile != null)
Utility.dropInventory(tile, tile.items); Utility.dropInventory(tile, tile.items);
super.onReplaced(state, worldIn, pos, newState, isMoving); super.onRemove(state, worldIn, pos, newState, isMoving);
} }
} }
@Nullable @org.jetbrains.annotations.Nullable
@Override @Override
public TileEntity createNewTileEntity(IBlockReader worldIn) { public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemTerminalTileEntity(); return new ItemTerminalBlockEntity(pos, state);
} }
@Override @Override
public BlockRenderType getRenderType(BlockState state) { public RenderShape getRenderShape(BlockState state) {
return BlockRenderType.MODEL; return RenderShape.MODEL;
} }
@Override @Override
public void addInformation(ItemStack stack, @Nullable IBlockReader worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) { public void appendHoverText(ItemStack stack, @Nullable BlockGetter worldIn, List<Component> tooltip, TooltipFlag flagIn) {
Utility.addTooltip(this.getRegistryName().getPath(), tooltip); Utility.addTooltip(this.getRegistryName().getPath(), tooltip);
} }
@org.jetbrains.annotations.Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
return createTickerHelper(type, Registry.itemTerminalBlockEntity, ItemTerminalBlockEntity::tick);
}
} }

View file

@ -13,27 +13,26 @@ import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketNetworkItems; import de.ellpeck.prettypipes.packets.PacketNetworkItems;
import de.ellpeck.prettypipes.pipe.ConnectionType; import de.ellpeck.prettypipes.pipe.ConnectionType;
import de.ellpeck.prettypipes.pipe.IPipeConnectable; import de.ellpeck.prettypipes.pipe.IPipeConnectable;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalContainer; import de.ellpeck.prettypipes.terminal.containers.ItemTerminalContainer;
import net.minecraft.block.BlockState; import net.minecraft.ChatFormatting;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.core.Direction;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.tileentity.ITickableTileEntity; import net.minecraft.nbt.Tag;
import net.minecraft.tileentity.TileEntity; import net.minecraft.network.chat.Component;
import net.minecraft.tileentity.TileEntityType; import net.minecraft.network.chat.Style;
import net.minecraft.util.Direction; import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.util.math.BlockPos; import net.minecraft.world.MenuProvider;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.util.text.Style; import net.minecraft.world.entity.player.Player;
import net.minecraft.util.text.TextFormatting; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.World; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler; import net.minecraftforge.items.ItemStackHandler;
@ -45,7 +44,7 @@ import java.util.*;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ItemTerminalTileEntity extends TileEntity implements INamedContainerProvider, ITickableTileEntity, IPipeConnectable { public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnectable, MenuProvider {
public final ItemStackHandler items = new ItemStackHandler(12) { public final ItemStackHandler items = new ItemStackHandler(12) {
@Override @Override
@ -57,65 +56,64 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
private final Queue<NetworkLock> existingRequests = new LinkedList<>(); private final Queue<NetworkLock> existingRequests = new LinkedList<>();
private final LazyOptional<IPipeConnectable> lazyThis = LazyOptional.of(() -> this); private final LazyOptional<IPipeConnectable> lazyThis = LazyOptional.of(() -> this);
protected ItemTerminalTileEntity(TileEntityType<?> tileEntityTypeIn) { public ItemTerminalBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(tileEntityTypeIn); super(type, pos, state);
} }
public ItemTerminalTileEntity() { public ItemTerminalBlockEntity(BlockPos pos, BlockState state) {
this(Registry.itemTerminalTileEntity); this(Registry.itemTerminalBlockEntity, pos, state);
} }
@Override public static void tick(Level level, BlockPos pos, BlockState state, ItemTerminalBlockEntity terminal) {
public void tick() { if (terminal.level.isClientSide)
if (this.world.isRemote)
return; return;
PipeNetwork network = PipeNetwork.get(this.world); var network = PipeNetwork.get(terminal.level);
PipeTileEntity pipe = this.getConnectedPipe(); var pipe = terminal.getConnectedPipe();
if (pipe == null) if (pipe == null)
return; return;
boolean update = false; var update = false;
int interval = pipe.pressurizer != null ? 2 : 10; var interval = pipe.pressurizer != null ? 2 : 10;
if (this.world.getGameTime() % interval == 0) { if (terminal.level.getGameTime() % interval == 0) {
for (int i = 6; i < 12; i++) { for (var i = 6; i < 12; i++) {
ItemStack extracted = this.items.extractItem(i, Integer.MAX_VALUE, true); var extracted = terminal.items.extractItem(i, Integer.MAX_VALUE, true);
if (extracted.isEmpty()) if (extracted.isEmpty())
continue; continue;
ItemStack remain = network.routeItem(pipe.getPos(), this.pos, extracted, true); var remain = network.routeItem(pipe.getBlockPos(), terminal.getBlockPos(), extracted, true);
if (remain.getCount() == extracted.getCount()) if (remain.getCount() == extracted.getCount())
continue; continue;
this.items.extractItem(i, extracted.getCount() - remain.getCount(), false); terminal.items.extractItem(i, extracted.getCount() - remain.getCount(), false);
break; break;
} }
if (!this.existingRequests.isEmpty()) { if (!terminal.existingRequests.isEmpty()) {
NetworkLock request = this.existingRequests.remove(); var request = terminal.existingRequests.remove();
network.resolveNetworkLock(request); network.resolveNetworkLock(request);
network.requestExistingItem(request.location, pipe.getPos(), this.pos, request, request.stack, ItemEquality.NBT); network.requestExistingItem(request.location, pipe.getBlockPos(), terminal.getBlockPos(), request, request.stack, ItemEquality.NBT);
update = true; update = true;
} }
} }
if (this.world.getGameTime() % 100 == 0 || update) { if (terminal.level.getGameTime() % 100 == 0 || update) {
PlayerEntity[] lookingPlayers = this.getLookingPlayers(); var lookingPlayers = terminal.getLookingPlayers();
if (lookingPlayers.length > 0) if (lookingPlayers.length > 0)
this.updateItems(lookingPlayers); terminal.updateItems(lookingPlayers);
} }
} }
@Override @Override
public void remove() { public void setRemoved() {
super.remove(); super.setRemoved();
PipeNetwork network = PipeNetwork.get(this.world); var network = PipeNetwork.get(this.level);
for (NetworkLock lock : this.existingRequests) for (var lock : this.existingRequests)
network.resolveNetworkLock(lock); network.resolveNetworkLock(lock);
this.lazyThis.invalidate(); this.lazyThis.invalidate();
} }
public String getInvalidTerminalReason() { public String getInvalidTerminalReason() {
PipeNetwork network = PipeNetwork.get(this.world); var network = PipeNetwork.get(this.level);
long pipes = Arrays.stream(Direction.values()) var pipes = Arrays.stream(Direction.values())
.map(d -> network.getPipe(this.pos.offset(d))) .map(d -> network.getPipe(this.worldPosition.relative(d)))
.filter(Objects::nonNull).count(); .filter(Objects::nonNull).count();
if (pipes <= 0) if (pipes <= 0)
return "info." + PrettyPipes.ID + ".no_pipe_connected"; return "info." + PrettyPipes.ID + ".no_pipe_connected";
@ -124,43 +122,42 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
return null; return null;
} }
public PipeTileEntity getConnectedPipe() { public PipeBlockEntity getConnectedPipe() {
PipeNetwork network = PipeNetwork.get(this.world); var network = PipeNetwork.get(this.level);
for (Direction dir : Direction.values()) { for (var dir : Direction.values()) {
PipeTileEntity pipe = network.getPipe(this.pos.offset(dir)); var pipe = network.getPipe(this.worldPosition.relative(dir));
if (pipe != null) if (pipe != null)
return pipe; return pipe;
} }
return null; return null;
} }
public void updateItems(PlayerEntity... playersToSync) { public void updateItems(Player... playersToSync) {
PipeTileEntity pipe = this.getConnectedPipe(); var pipe = this.getConnectedPipe();
if (pipe == null) if (pipe == null)
return; return;
this.networkItems = this.collectItems(ItemEquality.NBT); this.networkItems = this.collectItems(ItemEquality.NBT);
if (playersToSync.length > 0) { if (playersToSync.length > 0) {
List<ItemStack> clientItems = this.networkItems.values().stream().map(NetworkItem::asStack).collect(Collectors.toList()); var clientItems = this.networkItems.values().stream().map(NetworkItem::asStack).collect(Collectors.toList());
List<ItemStack> clientCraftables = PipeNetwork.get(this.world).getAllCraftables(pipe.getPos()).stream().map(Pair::getRight).collect(Collectors.toList()); var clientCraftables = PipeNetwork.get(this.level).getAllCraftables(pipe.getBlockPos()).stream().map(Pair::getRight).collect(Collectors.toList());
List<ItemStack> currentlyCrafting = this.getCurrentlyCrafting().stream().sorted(Comparator.comparingInt(ItemStack::getCount).reversed()).collect(Collectors.toList()); var currentlyCrafting = this.getCurrentlyCrafting().stream().sorted(Comparator.comparingInt(ItemStack::getCount).reversed()).collect(Collectors.toList());
for (PlayerEntity player : playersToSync) { for (var player : playersToSync) {
if (!(player.openContainer instanceof ItemTerminalContainer)) if (!(player.containerMenu instanceof ItemTerminalContainer container))
continue; continue;
ItemTerminalTileEntity tile = ((ItemTerminalContainer) player.openContainer).tile; if (container.tile != this)
if (tile != this)
continue; continue;
PacketHandler.sendTo(player, new PacketNetworkItems(clientItems, clientCraftables, currentlyCrafting)); PacketHandler.sendTo(player, new PacketNetworkItems(clientItems, clientCraftables, currentlyCrafting));
} }
} }
} }
public void requestItem(PlayerEntity player, ItemStack stack) { public void requestItem(Player player, ItemStack stack) {
PipeNetwork network = PipeNetwork.get(this.world); var network = PipeNetwork.get(this.level);
network.startProfile("terminal_request_item"); network.startProfile("terminal_request_item");
this.updateItems(); this.updateItems();
int requested = this.requestItemImpl(stack, onItemUnavailable(player)); var requested = this.requestItemImpl(stack, onItemUnavailable(player));
if (requested > 0) { if (requested > 0) {
player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".sending", requested, stack.getDisplayName()).setStyle(Style.EMPTY.setFormatting(TextFormatting.GREEN)), UUID.randomUUID()); player.sendMessage(new TranslatableComponent("info." + PrettyPipes.ID + ".sending", requested, stack.getDisplayName()).setStyle(Style.EMPTY.applyFormat(ChatFormatting.GREEN)), UUID.randomUUID());
} else { } else {
onItemUnavailable(player).accept(stack); onItemUnavailable(player).accept(stack);
} }
@ -168,32 +165,29 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
} }
public int requestItemImpl(ItemStack stack, Consumer<ItemStack> unavailableConsumer) { public int requestItemImpl(ItemStack stack, Consumer<ItemStack> unavailableConsumer) {
NetworkItem item = this.networkItems.get(new EquatableItemStack(stack, ItemEquality.NBT)); var item = this.networkItems.get(new EquatableItemStack(stack, ItemEquality.NBT));
Collection<NetworkLocation> locations = item == null ? Collections.emptyList() : item.getLocations(); Collection<NetworkLocation> locations = item == null ? Collections.emptyList() : item.getLocations();
Pair<List<NetworkLock>, ItemStack> ret = requestItemLater(this.world, this.getConnectedPipe().getPos(), locations, unavailableConsumer, stack, new Stack<>(), ItemEquality.NBT); var ret = requestItemLater(this.level, this.getConnectedPipe().getBlockPos(), locations, unavailableConsumer, stack, new Stack<>(), ItemEquality.NBT);
this.existingRequests.addAll(ret.getLeft()); this.existingRequests.addAll(ret.getLeft());
return stack.getCount() - ret.getRight().getCount(); return stack.getCount() - ret.getRight().getCount();
} }
protected PlayerEntity[] getLookingPlayers() { protected Player[] getLookingPlayers() {
return this.world.getPlayers().stream() return this.level.players().stream().filter(p -> p.containerMenu instanceof ItemTerminalContainer container && container.tile == this).toArray(Player[]::new);
.filter(p -> p.openContainer instanceof ItemTerminalContainer)
.filter(p -> ((ItemTerminalContainer) p.openContainer).tile == this)
.toArray(PlayerEntity[]::new);
} }
private Map<EquatableItemStack, NetworkItem> collectItems(ItemEquality... equalityTypes) { private Map<EquatableItemStack, NetworkItem> collectItems(ItemEquality... equalityTypes) {
PipeNetwork network = PipeNetwork.get(this.world); var network = PipeNetwork.get(this.level);
network.startProfile("terminal_collect_items"); network.startProfile("terminal_collect_items");
PipeTileEntity pipe = this.getConnectedPipe(); var pipe = this.getConnectedPipe();
Map<EquatableItemStack, NetworkItem> items = new HashMap<>(); Map<EquatableItemStack, NetworkItem> items = new HashMap<>();
for (NetworkLocation location : network.getOrderedNetworkItems(pipe.getPos())) { for (var location : network.getOrderedNetworkItems(pipe.getBlockPos())) {
for (Map.Entry<Integer, ItemStack> entry : location.getItems(this.world).entrySet()) { for (var entry : location.getItems(this.level).entrySet()) {
// make sure we can extract from this slot to display it // make sure we can extract from this slot to display it
if (!location.canExtract(this.world, entry.getKey())) if (!location.canExtract(this.level, entry.getKey()))
continue; continue;
EquatableItemStack equatable = new EquatableItemStack(entry.getValue(), equalityTypes); var equatable = new EquatableItemStack(entry.getValue(), equalityTypes);
NetworkItem item = items.computeIfAbsent(equatable, NetworkItem::new); var item = items.computeIfAbsent(equatable, NetworkItem::new);
item.add(location, entry.getValue()); item.add(location, entry.getValue());
} }
} }
@ -202,57 +196,57 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
} }
private List<ItemStack> getCurrentlyCrafting() { private List<ItemStack> getCurrentlyCrafting() {
PipeNetwork network = PipeNetwork.get(this.world); var network = PipeNetwork.get(this.level);
PipeTileEntity pipe = this.getConnectedPipe(); var pipe = this.getConnectedPipe();
if (pipe == null) if (pipe == null)
return Collections.emptyList(); return Collections.emptyList();
List<Pair<BlockPos, ItemStack>> crafting = network.getCurrentlyCrafting(pipe.getPos()); var crafting = network.getCurrentlyCrafting(pipe.getBlockPos());
return crafting.stream().map(Pair::getRight).collect(Collectors.toList()); return crafting.stream().map(Pair::getRight).collect(Collectors.toList());
} }
public void cancelCrafting() { public void cancelCrafting() {
PipeNetwork network = PipeNetwork.get(this.world); var network = PipeNetwork.get(this.level);
PipeTileEntity pipe = this.getConnectedPipe(); var pipe = this.getConnectedPipe();
if (pipe == null) if (pipe == null)
return; return;
for (Pair<BlockPos, ItemStack> craftable : network.getAllCraftables(pipe.getPos())) { for (var craftable : network.getAllCraftables(pipe.getBlockPos())) {
PipeTileEntity otherPipe = network.getPipe(craftable.getLeft()); var otherPipe = network.getPipe(craftable.getLeft());
if (otherPipe != null) { if (otherPipe != null) {
for (NetworkLock lock : otherPipe.craftIngredientRequests) for (var lock : otherPipe.craftIngredientRequests)
network.resolveNetworkLock(lock); network.resolveNetworkLock(lock);
otherPipe.craftIngredientRequests.clear(); otherPipe.craftIngredientRequests.clear();
otherPipe.craftResultRequests.clear(); otherPipe.craftResultRequests.clear();
} }
} }
PlayerEntity[] lookingPlayers = this.getLookingPlayers(); var lookingPlayers = this.getLookingPlayers();
if (lookingPlayers.length > 0) if (lookingPlayers.length > 0)
this.updateItems(lookingPlayers); this.updateItems(lookingPlayers);
} }
@Override @Override
public CompoundTag write(CompoundTag compound) { public CompoundTag save(CompoundTag compound) {
compound.put("items", this.items.serializeNBT()); compound.put("items", this.items.serializeNBT());
compound.put("requests", Utility.serializeAll(this.existingRequests)); compound.put("requests", Utility.serializeAll(this.existingRequests));
return super.write(compound); return super.save(compound);
} }
@Override @Override
public void read(BlockState state, CompoundTag compound) { public void load(CompoundTag compound) {
this.items.deserializeNBT(compound.getCompound("items")); this.items.deserializeNBT(compound.getCompound("items"));
this.existingRequests.clear(); this.existingRequests.clear();
this.existingRequests.addAll(Utility.deserializeAll(compound.getList("requests", NBT.TAG_COMPOUND), NetworkLock::new)); this.existingRequests.addAll(Utility.deserializeAll(compound.getList("requests", Tag.TAG_COMPOUND), NetworkLock::new));
super.read(state, compound); super.load(compound);
} }
@Override @Override
public ITextComponent getDisplayName() { public Component getDisplayName() {
return new TranslationTextComponent("container." + PrettyPipes.ID + ".item_terminal"); return new TranslatableComponent("container." + PrettyPipes.ID + ".item_terminal");
} }
@Nullable @Nullable
@Override @Override
public Container createMenu(int window, PlayerInventory inv, PlayerEntity player) { public AbstractContainerMenu createMenu(int window, Inventory inv, Player player) {
return new ItemTerminalContainer(Registry.itemTerminalContainer, window, player, this.pos); return new ItemTerminalContainer(Registry.itemTerminalContainer, window, player, this.worldPosition);
} }
@Override @Override
@ -269,8 +263,8 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
@Override @Override
public ItemStack insertItem(BlockPos pipePos, Direction direction, ItemStack stack, boolean simulate) { public ItemStack insertItem(BlockPos pipePos, Direction direction, ItemStack stack, boolean simulate) {
BlockPos pos = pipePos.offset(direction); var pos = pipePos.relative(direction);
ItemTerminalTileEntity tile = Utility.getBlockEntity(ItemTerminalTileEntity.class, world, pos); var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, this.level, pos);
if (tile != null) if (tile != null)
return ItemHandlerHelper.insertItemStacked(tile.items, stack, simulate); return ItemHandlerHelper.insertItemStacked(tile.items, stack, simulate);
return stack; return stack;
@ -281,13 +275,13 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
return true; return true;
} }
public static Pair<List<NetworkLock>, ItemStack> requestItemLater(World world, BlockPos destPipe, Collection<NetworkLocation> locations, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) { public static Pair<List<NetworkLock>, ItemStack> requestItemLater(Level world, BlockPos destPipe, Collection<NetworkLocation> locations, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) {
List<NetworkLock> requests = new ArrayList<>(); List<NetworkLock> requests = new ArrayList<>();
ItemStack remain = stack.copy(); var remain = stack.copy();
PipeNetwork network = PipeNetwork.get(world); var network = PipeNetwork.get(world);
// check for existing items // check for existing items
for (NetworkLocation location : locations) { for (var location : locations) {
int amount = location.getItemAmount(world, stack, equalityTypes); var amount = location.getItemAmount(world, stack, equalityTypes);
if (amount <= 0) if (amount <= 0)
continue; continue;
amount -= network.getLockedAmount(location.getPos(), stack, null, equalityTypes); amount -= network.getLockedAmount(location.getPos(), stack, null, equalityTypes);
@ -296,9 +290,9 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
amount = remain.getCount(); amount = remain.getCount();
remain.shrink(amount); remain.shrink(amount);
while (amount > 0) { while (amount > 0) {
ItemStack copy = stack.copy(); var copy = stack.copy();
copy.setCount(Math.min(stack.getMaxStackSize(), amount)); copy.setCount(Math.min(stack.getMaxStackSize(), amount));
NetworkLock lock = new NetworkLock(location, copy); var lock = new NetworkLock(location, copy);
network.createNetworkLock(lock); network.createNetworkLock(lock);
requests.add(lock); requests.add(lock);
amount -= copy.getCount(); amount -= copy.getCount();
@ -313,7 +307,7 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
return Pair.of(requests, remain); return Pair.of(requests, remain);
} }
public static Consumer<ItemStack> onItemUnavailable(PlayerEntity player) { public static Consumer<ItemStack> onItemUnavailable(Player player) {
return s -> player.sendMessage(new TranslationTextComponent("info." + PrettyPipes.ID + ".not_found", s.getDisplayName()).setStyle(Style.EMPTY.setFormatting(TextFormatting.RED)), UUID.randomUUID()); return s -> player.sendMessage(new TranslatableComponent("info." + PrettyPipes.ID + ".not_found", s.getDisplayName()).setStyle(Style.EMPTY.applyFormat(ChatFormatting.RED)), UUID.randomUUID());
} }
} }

View file

@ -1,64 +1,57 @@
package de.ellpeck.prettypipes.terminal.containers; package de.ellpeck.prettypipes.terminal.containers;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.terminal.CraftingTerminalTileEntity; import de.ellpeck.prettypipes.terminal.CraftingTerminalBlockEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
import net.minecraft.inventory.CraftResultInventory; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.inventory.CraftingInventory; import net.minecraft.world.Container;
import net.minecraft.inventory.IInventory; import net.minecraft.world.entity.player.Player;
import net.minecraft.inventory.container.ClickType; import net.minecraft.world.inventory.*;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.inventory.container.CraftingResultSlot;
import net.minecraft.inventory.container.Slot;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.item.crafting.ICraftingRecipe; import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.network.play.server.SSetSlotPacket;
import net.minecraft.util.math.BlockPos;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Optional;
public class CraftingTerminalContainer extends ItemTerminalContainer { public class CraftingTerminalContainer extends ItemTerminalContainer {
public CraftingInventory craftInventory; public CraftingContainer craftInventory;
public CraftResultInventory craftResult; public ResultContainer craftResult;
private final PlayerEntity player; private final Player player;
public CraftingTerminalContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, BlockPos pos) { public CraftingTerminalContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos) {
super(type, id, player, pos); super(type, id, player, pos);
this.player = player; this.player = player;
this.onCraftMatrixChanged(this.craftInventory); this.slotsChanged(this.craftInventory);
} }
@Override @Override
protected void addOwnSlots(PlayerEntity player) { protected void addDataSlots(ContainerData data) {
this.craftInventory = new WrappedCraftingInventory(this.getTile().craftItems, this, 3, 3); this.craftInventory = new WrappedCraftingInventory(this.getTile().craftItems, this, 3, 3);
this.craftResult = new CraftResultInventory(); this.craftResult = new ResultContainer();
this.addSlot(new CraftingResultSlot(player, this.craftInventory, this.craftResult, 0, 25, 77)); this.addSlot(new ResultSlot(this.player, this.craftInventory, this.craftResult, 0, 25, 77));
for (int i = 0; i < 3; i++) for (var i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) for (var j = 0; j < 3; j++)
this.addSlot(new Slot(this.craftInventory, j + i * 3, 7 + j * 18, 18 + i * 18)); this.addSlot(new Slot(this.craftInventory, j + i * 3, 7 + j * 18, 18 + i * 18));
super.addOwnSlots(player); super.addDataSlots(data);
} }
@Override @Override
public void onCraftMatrixChanged(IInventory inventoryIn) { public void slotsChanged(Container inventoryIn) {
if (!this.player.world.isRemote) { if (!this.player.level.isClientSide) {
ItemStack ret = ItemStack.EMPTY; var ret = ItemStack.EMPTY;
Optional<ICraftingRecipe> optional = this.player.world.getServer().getRecipeManager().getRecipe(IRecipeType.CRAFTING, this.craftInventory, this.player.world); var optional = this.player.level.getServer().getRecipeManager().getRecipeFor(RecipeType.CRAFTING, this.craftInventory, this.player.level);
if (optional.isPresent()) if (optional.isPresent())
ret = optional.get().getCraftingResult(this.craftInventory); ret = optional.get().assemble(this.craftInventory);
this.craftResult.setInventorySlotContents(0, ret); this.craftResult.setItem(0, ret);
((ServerPlayerEntity) this.player).connection.sendPacket(new SSetSlotPacket(this.windowId, 0, ret)); ((ServerPlayer) this.player).connection.send(new ClientboundContainerSetSlotPacket(this.containerId, 0, 0, ret));
} }
} }
@Override @Override
public ItemStack transferStackInSlot(PlayerEntity player, int slotIndex) { public ItemStack quickMoveStack(Player player, int slotIndex) {
return Utility.transferStackInSlot(this, this::mergeItemStack, player, slotIndex, stack -> Pair.of(6 + 10, 12 + 10)); return Utility.transferStackInSlot(this, this::moveItemStackTo, player, slotIndex, stack -> Pair.of(6 + 10, 12 + 10));
} }
@Override @Override
@ -67,16 +60,16 @@ public class CraftingTerminalContainer extends ItemTerminalContainer {
} }
@Override @Override
public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) { public void clicked(int slotId, int dragType, ClickType clickTypeIn, Player player) {
if (slotId > 0 && clickTypeIn == ClickType.PICKUP) { if (slotId > 0 && clickTypeIn == ClickType.PICKUP) {
Slot slot = this.inventorySlots.get(slotId); var slot = this.slots.get(slotId);
if (slot.inventory == this.craftInventory && !slot.getHasStack()) if (slot.container == this.craftInventory && !slot.hasItem())
this.getTile().ghostItems.setStackInSlot(slot.getSlotIndex(), ItemStack.EMPTY); this.getTile().ghostItems.setStackInSlot(slot.getSlotIndex(), ItemStack.EMPTY);
} }
return super.slotClick(slotId, dragType, clickTypeIn, player); super.clicked(slotId, dragType, clickTypeIn, player);
} }
public CraftingTerminalTileEntity getTile() { public CraftingTerminalBlockEntity getTile() {
return (CraftingTerminalTileEntity) this.tile; return (CraftingTerminalBlockEntity) this.tile;
} }
} }

View file

@ -1,49 +1,42 @@
package de.ellpeck.prettypipes.terminal.containers; package de.ellpeck.prettypipes.terminal.containers;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.packets.PacketButton; import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler; import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketRequest; import net.minecraft.client.gui.components.Button;
import de.ellpeck.prettypipes.terminal.CraftingTerminalTileEntity; import net.minecraft.network.chat.Component;
import net.minecraft.client.gui.widget.Widget; import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.util.text.TranslationTextComponent;
public class CraftingTerminalGui extends ItemTerminalGui { public class CraftingTerminalGui extends ItemTerminalGui {
private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/crafting_terminal.png"); private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/crafting_terminal.png");
private Button requestButton; private Button requestButton;
public CraftingTerminalGui(ItemTerminalContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) { public CraftingTerminalGui(ItemTerminalContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
this.xSize = 256; this.imageWidth = 256;
} }
@Override @Override
protected void init() { protected void init() {
super.init(); super.init();
this.requestButton = this.addButton(new Button(this.guiLeft + 8, this.guiTop + 100, 50, 20, new TranslationTextComponent("info." + PrettyPipes.ID + ".request"), button -> { this.requestButton = this.addRenderableWidget(new Button(this.leftPos + 8, this.topPos + 100, 50, 20, new TranslatableComponent("info." + PrettyPipes.ID + ".request"), button -> {
int amount = requestModifier(); var amount = requestModifier();
PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.CRAFT_TERMINAL_REQUEST, amount)); PacketHandler.sendToServer(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CRAFT_TERMINAL_REQUEST, amount));
})); }));
this.tick(); this.tick();
} }
@Override @Override
public void tick() { public void containerTick() {
super.tick(); super.containerTick();
CraftingTerminalTileEntity tile = this.getCraftingContainer().getTile(); var tile = this.getCraftingContainer().getTile();
this.requestButton.active = false; this.requestButton.active = false;
for (int i = 0; i < tile.craftItems.getSlots(); i++) { for (var i = 0; i < tile.craftItems.getSlots(); i++) {
ItemStack stack = tile.getRequestedCraftItem(i); var stack = tile.getRequestedCraftItem(i);
if (!stack.isEmpty() && stack.getCount() < stack.getMaxStackSize()) { if (!stack.isEmpty() && stack.getCount() < stack.getMaxStackSize()) {
this.requestButton.active = true; this.requestButton.active = true;
break; break;
@ -52,23 +45,23 @@ public class CraftingTerminalGui extends ItemTerminalGui {
} }
@Override @Override
protected void drawGuiContainerForegroundLayer(MatrixStack matrix, int mouseX, int mouseY) { protected void renderLabels(PoseStack matrix, int mouseX, int mouseY) {
super.drawGuiContainerForegroundLayer(matrix, mouseX, mouseY); super.renderLabels(matrix, mouseX, mouseY);
CraftingTerminalContainer container = this.getCraftingContainer(); var container = this.getCraftingContainer();
CraftingTerminalTileEntity tile = container.getTile(); var tile = container.getTile();
for (int i = 0; i < tile.ghostItems.getSlots(); i++) { for (var i = 0; i < tile.ghostItems.getSlots(); i++) {
if (!tile.craftItems.getStackInSlot(i).isEmpty()) if (!tile.craftItems.getStackInSlot(i).isEmpty())
continue; continue;
ItemStack ghost = tile.ghostItems.getStackInSlot(i); var ghost = tile.ghostItems.getStackInSlot(i);
if (ghost.isEmpty()) if (ghost.isEmpty())
continue; continue;
int finalI = i; var finalI = i;
Slot slot = container.inventorySlots.stream().filter(s -> s.inventory == container.craftInventory && s.getSlotIndex() == finalI).findFirst().orElse(null); var slot = container.slots.stream().filter(s -> s.container == container.craftInventory && s.getSlotIndex() == finalI).findFirst().orElse(null);
if (slot == null) if (slot == null)
continue; continue;
this.minecraft.getItemRenderer().renderItemIntoGUI(ghost, slot.xPos, slot.yPos); this.minecraft.getItemRenderer().renderGuiItem(ghost, slot.x, slot.y);
this.minecraft.getItemRenderer().renderItemOverlayIntoGUI(this.font, ghost, slot.xPos, slot.yPos, "0"); this.minecraft.getItemRenderer().renderGuiItemDecorations(this.font, ghost, slot.x, slot.y, "0");
} }
} }
@ -83,6 +76,6 @@ public class CraftingTerminalGui extends ItemTerminalGui {
} }
protected CraftingTerminalContainer getCraftingContainer() { protected CraftingTerminalContainer getCraftingContainer() {
return (CraftingTerminalContainer) this.container; return (CraftingTerminalContainer) this.menu;
} }
} }

View file

@ -1,44 +1,41 @@
package de.ellpeck.prettypipes.terminal.containers; package de.ellpeck.prettypipes.terminal.containers;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.terminal.ItemTerminalTileEntity; import de.ellpeck.prettypipes.terminal.ItemTerminalBlockEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.core.BlockPos;
import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.world.entity.player.Player;
import net.minecraft.inventory.container.ClickType; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.inventory.container.Container; import net.minecraft.world.inventory.MenuType;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.world.inventory.Slot;
import net.minecraft.inventory.container.Slot;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.items.SlotItemHandler; import net.minecraftforge.items.SlotItemHandler;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class ItemTerminalContainer extends Container { public class ItemTerminalContainer extends AbstractContainerMenu {
public final ItemTerminalTileEntity tile; public final ItemTerminalBlockEntity tile;
public ItemTerminalContainer(@Nullable ContainerType<?> type, int id, PlayerEntity player, BlockPos pos) { public ItemTerminalContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos) {
super(type, id); super(type, id);
this.tile = Utility.getBlockEntity(ItemTerminalTileEntity.class, player.world, pos); this.tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, player.level, pos);
this.addOwnSlots(player); this.addOwnSlots(player);
int off = this.getSlotXOffset(); var off = this.getSlotXOffset();
for (int l = 0; l < 3; ++l) for (var l = 0; l < 3; ++l)
for (int j1 = 0; j1 < 9; ++j1) for (var j1 = 0; j1 < 9; ++j1)
this.addSlot(new Slot(player.inventory, j1 + l * 9 + 9, 8 + off + j1 * 18, 154 + l * 18)); this.addSlot(new Slot(player.getInventory(), j1 + l * 9 + 9, 8 + off + j1 * 18, 154 + l * 18));
for (int i1 = 0; i1 < 9; ++i1) for (var i1 = 0; i1 < 9; ++i1)
this.addSlot(new Slot(player.inventory, i1, 8 + off + i1 * 18, 212)); this.addSlot(new Slot(player.getInventory(), i1, 8 + off + i1 * 18, 212));
} }
protected void addOwnSlots(PlayerEntity player) { protected void addOwnSlots(Player player) {
int off = this.getSlotXOffset(); var off = this.getSlotXOffset();
for (int i = 0; i < 6; i++) for (var i = 0; i < 6; i++)
this.addSlot(new SlotItemHandler(this.tile.items, i, 8 + off + i % 3 * 18, 102 + i / 3 * 18)); this.addSlot(new SlotItemHandler(this.tile.items, i, 8 + off + i % 3 * 18, 102 + i / 3 * 18));
for (int i = 0; i < 6; i++) for (var i = 0; i < 6; i++)
this.addSlot(new SlotItemHandler(this.tile.items, i + 6, 116 + off + i % 3 * 18, 102 + i / 3 * 18)); this.addSlot(new SlotItemHandler(this.tile.items, i + 6, 116 + off + i % 3 * 18, 102 + i / 3 * 18));
} }
@ -47,12 +44,12 @@ public class ItemTerminalContainer extends Container {
} }
@Override @Override
public ItemStack transferStackInSlot(PlayerEntity player, int slotIndex) { public ItemStack quickMoveStack(Player player, int slotIndex) {
return Utility.transferStackInSlot(this, this::mergeItemStack, player, slotIndex, stack -> Pair.of(6, 12)); return Utility.transferStackInSlot(this, this::moveItemStackTo, player, slotIndex, stack -> Pair.of(6, 12));
} }
@Override @Override
public boolean canInteractWith(PlayerEntity playerIn) { public boolean stillValid(Player player) {
return true; return true;
} }
} }

View file

@ -1,6 +1,7 @@
package de.ellpeck.prettypipes.terminal.containers; package de.ellpeck.prettypipes.terminal.containers;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.platform.InputConstants;
import com.mojang.blaze3d.vertex.PoseStack;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.misc.ItemTerminalWidget; import de.ellpeck.prettypipes.misc.ItemTerminalWidget;
import de.ellpeck.prettypipes.misc.PlayerPrefs; import de.ellpeck.prettypipes.misc.PlayerPrefs;
@ -8,31 +9,29 @@ import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler; import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketRequest; import de.ellpeck.prettypipes.packets.PacketRequest;
import joptsimple.internal.Strings; import joptsimple.internal.Strings;
import net.minecraft.client.gui.screen.inventory.ContainerScreen; import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.widget.Widget; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.widget.button.Button; import net.minecraft.client.resources.language.I18n;
import net.minecraft.client.resources.I18n; import net.minecraft.network.chat.Component;
import net.minecraft.client.util.InputMappings; import net.minecraft.network.chat.TextComponent;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.item.ItemStack;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.Mth;
import net.minecraft.util.text.ITextComponent; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.util.text.StringTextComponent; import net.minecraft.world.item.ItemStack;
import net.minecraft.util.text.TranslationTextComponent;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> { public class ItemTerminalGui extends AbstractContainerScreen<ItemTerminalContainer> {
private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/item_terminal.png"); private static final ResourceLocation TEXTURE = new ResourceLocation(PrettyPipes.ID, "textures/gui/item_terminal.png");
public List<ItemStack> currentlyCrafting; public List<ItemStack> currentlyCrafting;
public TextFieldWidget search; public EditBox search;
// craftables have the second parameter set to true // craftables have the second parameter set to true
private final List<Pair<ItemStack, Boolean>> sortedItems = new ArrayList<>(); private final List<Pair<ItemStack, Boolean>> sortedItems = new ArrayList<>();
@ -50,24 +49,24 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
private ItemStack hoveredCrafting; private ItemStack hoveredCrafting;
private boolean isScrolling; private boolean isScrolling;
public ItemTerminalGui(ItemTerminalContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) { public ItemTerminalGui(ItemTerminalContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn); super(screenContainer, inv, titleIn);
this.xSize = 176 + 15; this.imageWidth = 176 + 15;
this.ySize = 236; this.imageHeight = 236;
} }
@Override @Override
protected void init() { protected void init() {
super.init(); super.init();
this.search = this.addButton(new TextFieldWidget(this.font, this.guiLeft + this.getXOffset() + 97, this.guiTop + 6, 86, 8, new StringTextComponent(""))); this.search = this.addRenderableWidget(new EditBox(this.font, this.leftPos + this.getXOffset() + 97, this.topPos + 6, 86, 8, new TextComponent("")));
this.search.setEnableBackgroundDrawing(false); this.search.setBordered(false);
this.lastSearchText = ""; this.lastSearchText = "";
if (this.items != null) if (this.items != null)
this.updateWidgets(); this.updateWidgets();
this.plusButton = this.addButton(new Button(this.guiLeft + this.getXOffset() + 95 - 7 + 12, this.guiTop + 103, 12, 12, new StringTextComponent("+"), button -> { this.plusButton = this.addRenderableWidget(new Button(this.leftPos + this.getXOffset() + 95 - 7 + 12, this.topPos + 103, 12, 12, new TextComponent("+"), button -> {
int modifier = requestModifier(); var modifier = requestModifier();
if (modifier > 1 && this.requestAmount == 1) { if (modifier > 1 && this.requestAmount == 1) {
this.requestAmount = modifier; this.requestAmount = modifier;
} else { } else {
@ -77,44 +76,44 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
if (this.requestAmount > 384) if (this.requestAmount > 384)
this.requestAmount = 384; this.requestAmount = 384;
})); }));
this.minusButton = this.addButton(new Button(this.guiLeft + this.getXOffset() + 95 - 7 - 24, this.guiTop + 103, 12, 12, new StringTextComponent("-"), button -> { this.minusButton = this.addRenderableWidget(new Button(this.leftPos + this.getXOffset() + 95 - 7 - 24, this.topPos + 103, 12, 12, new TextComponent("-"), button -> {
this.requestAmount -= requestModifier(); this.requestAmount -= requestModifier();
if (this.requestAmount < 1) if (this.requestAmount < 1)
this.requestAmount = 1; this.requestAmount = 1;
})); }));
this.minusButton.active = false; this.minusButton.active = false;
this.requestButton = this.addButton(new Button(this.guiLeft + this.getXOffset() + 95 - 7 - 25, this.guiTop + 115, 50, 20, new TranslationTextComponent("info." + PrettyPipes.ID + ".request"), button -> { this.requestButton = this.addRenderableWidget(new Button(this.leftPos + this.getXOffset() + 95 - 7 - 25, this.topPos + 115, 50, 20, new TranslatableComponent("info." + PrettyPipes.ID + ".request"), button -> {
Optional<ItemTerminalWidget> widget = this.streamWidgets().filter(w -> w.selected).findFirst(); var widget = this.streamWidgets().filter(w -> w.selected).findFirst();
if (!widget.isPresent()) if (!widget.isPresent())
return; return;
ItemStack stack = widget.get().stack.copy(); var stack = widget.get().stack.copy();
stack.setCount(1); stack.setCount(1);
PacketHandler.sendToServer(new PacketRequest(this.container.tile.getPos(), stack, this.requestAmount)); PacketHandler.sendToServer(new PacketRequest(this.menu.tile.getBlockPos(), stack, this.requestAmount));
this.requestAmount = 1; this.requestAmount = 1;
})); }));
this.requestButton.active = false; this.requestButton.active = false;
this.orderButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop, 20, 20, new StringTextComponent(""), button -> { this.orderButton = this.addRenderableWidget(new Button(this.leftPos - 22, this.topPos, 20, 20, new TextComponent(""), button -> {
if (this.sortedItems == null) if (this.sortedItems == null)
return; return;
PlayerPrefs prefs = PlayerPrefs.get(); var prefs = PlayerPrefs.get();
prefs.terminalItemOrder = prefs.terminalItemOrder.next(); prefs.terminalItemOrder = prefs.terminalItemOrder.next();
prefs.save(); prefs.save();
this.updateWidgets(); this.updateWidgets();
})); }));
this.ascendingButton = this.addButton(new Button(this.guiLeft - 22, this.guiTop + 22, 20, 20, new StringTextComponent(""), button -> { this.ascendingButton = this.addRenderableWidget(new Button(this.leftPos - 22, this.topPos + 22, 20, 20, new TextComponent(""), button -> {
if (this.sortedItems == null) if (this.sortedItems == null)
return; return;
PlayerPrefs prefs = PlayerPrefs.get(); var prefs = PlayerPrefs.get();
prefs.terminalAscending = !prefs.terminalAscending; prefs.terminalAscending = !prefs.terminalAscending;
prefs.save(); prefs.save();
this.updateWidgets(); this.updateWidgets();
})); }));
this.cancelCraftingButton = this.addButton(new Button(this.guiLeft + this.xSize + 4, this.guiTop + 4 + 64, 54, 20, new TranslationTextComponent("info." + PrettyPipes.ID + ".cancel_all"), b -> { this.cancelCraftingButton = this.addRenderableWidget(new Button(this.leftPos + this.imageWidth + 4, this.topPos + 4 + 64, 54, 20, new TranslatableComponent("info." + PrettyPipes.ID + ".cancel_all"), b -> {
})); }));
this.cancelCraftingButton.visible = false; this.cancelCraftingButton.visible = false;
for (int y = 0; y < 4; y++) { for (var y = 0; y < 4; y++) {
for (int x = 0; x < 9; x++) for (var x = 0; x < 9; x++)
this.addButton(new ItemTerminalWidget(this.guiLeft + this.getXOffset() + 8 + x * 18, this.guiTop + 18 + y * 18, x, y, this)); this.addRenderableWidget(new ItemTerminalWidget(this.leftPos + this.getXOffset() + 8 + x * 18, this.topPos + 18 + y * 18, x, y, this));
} }
} }
@ -123,15 +122,15 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
} }
@Override @Override
public void tick() { public void containerTick() {
super.tick(); super.containerTick();
this.requestButton.active = this.streamWidgets().anyMatch(w -> w.selected); this.requestButton.active = this.streamWidgets().anyMatch(w -> w.selected);
this.plusButton.active = this.requestAmount < 384; this.plusButton.active = this.requestAmount < 384;
this.minusButton.active = this.requestAmount > 1; this.minusButton.active = this.requestAmount > 1;
this.search.tick(); this.search.tick();
if (this.items != null) { if (this.items != null) {
String text = this.search.getText(); var text = this.search.getValue();
if (!this.lastSearchText.equals(text)) { if (!this.lastSearchText.equals(text)) {
this.lastSearchText = text; this.lastSearchText = text;
this.updateWidgets(); this.updateWidgets();
@ -141,7 +140,7 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
@Override @Override
public boolean mouseClicked(double mouseX, double mouseY, int button) { public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (button == 0 && mouseX >= this.guiLeft + this.getXOffset() + 172 && this.guiTop + mouseY >= 18 && mouseX < this.guiLeft + this.getXOffset() + 172 + 12 && mouseY < this.guiTop + 18 + 70) { if (button == 0 && mouseX >= this.leftPos + this.getXOffset() + 172 && this.topPos + mouseY >= 18 && mouseX < this.leftPos + this.getXOffset() + 172 + 12 && mouseY < this.topPos + 18 + 70) {
this.isScrolling = true; this.isScrolling = true;
return true; return true;
} }
@ -152,9 +151,9 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
public boolean mouseReleased(double mouseX, double mouseY, int button) { public boolean mouseReleased(double mouseX, double mouseY, int button) {
// we have to do the click logic here because JEI is activated when letting go of the mouse button // we have to do the click logic here because JEI is activated when letting go of the mouse button
// and vanilla buttons are activated when the click starts, so we'll always invoke jei accidentally by default // and vanilla buttons are activated when the click starts, so we'll always invoke jei accidentally by default
if (button == 0 && this.cancelCraftingButton.visible && this.cancelCraftingButton.isHovered()) { if (button == 0 && this.cancelCraftingButton.visible && this.cancelCraftingButton.isHoveredOrFocused()) {
if (this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty()) { if (this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty()) {
PacketHandler.sendToServer(new PacketButton(this.container.tile.getPos(), PacketButton.ButtonResult.CANCEL_CRAFTING)); PacketHandler.sendToServer(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CANCEL_CRAFTING));
return true; return true;
} }
@ -163,7 +162,7 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
this.isScrolling = false; this.isScrolling = false;
else if (button == 1 && mouseX >= this.search.x && mouseX <= this.search.x + this.search.getWidth() && mouseY >= this.search.y && mouseY <= this.search.y + 8) { else if (button == 1 && mouseX >= this.search.x && mouseX <= this.search.x + this.search.getWidth() && mouseY >= this.search.y && mouseY <= this.search.y + 8) {
//clear text from search field when letting go of right mouse button within search field //clear text from search field when letting go of right mouse button within search field
this.search.setText(""); this.search.setValue("");
return true; return true;
} }
return super.mouseReleased(mouseX, mouseY, button); return super.mouseReleased(mouseX, mouseY, button);
@ -172,8 +171,8 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
@Override @Override
public boolean mouseDragged(double mouseX, double mouseY, int i, double j, double k) { public boolean mouseDragged(double mouseX, double mouseY, int i, double j, double k) {
if (this.isScrolling) { if (this.isScrolling) {
float percentage = MathHelper.clamp(((float) mouseY - (this.guiTop + 18) - 7.5F) / (70 - 15), 0, 1); var percentage = Mth.clamp(((float) mouseY - (this.topPos + 18) - 7.5F) / (70 - 15), 0, 1);
int offset = (int) (percentage * (float) (this.sortedItems.size() / 9 - 3)); var offset = (int) (percentage * (float) (this.sortedItems.size() / 9 - 3));
if (offset != this.scrollOffset) { if (offset != this.scrollOffset) {
this.scrollOffset = offset; this.scrollOffset = offset;
this.updateWidgets(); this.updateWidgets();
@ -187,8 +186,8 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
public boolean keyPressed(int x, int y, int z) { public boolean keyPressed(int x, int y, int z) {
// for some reason we have to do this to make the text field allow the inventory key to be typed // for some reason we have to do this to make the text field allow the inventory key to be typed
if (this.search.isFocused()) { if (this.search.isFocused()) {
InputMappings.Input mouseKey = InputMappings.getInputByCode(x, y); var mouseKey = InputConstants.getKey(x, y);
if (this.minecraft.gameSettings.keyBindInventory.isActiveAndMatches(mouseKey)) if (this.minecraft.options.keyInventory.isActiveAndMatches(mouseKey))
return false; return false;
} }
return super.keyPressed(x, y, z); return super.keyPressed(x, y, z);
@ -202,30 +201,30 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
} }
public void updateWidgets() { public void updateWidgets() {
PlayerPrefs prefs = PlayerPrefs.get(); var prefs = PlayerPrefs.get();
this.ascendingButton.setMessage(new StringTextComponent(prefs.terminalAscending ? "^" : "v")); this.ascendingButton.setMessage(new TextComponent(prefs.terminalAscending ? "^" : "v"));
this.orderButton.setMessage(new StringTextComponent(prefs.terminalItemOrder.name().substring(0, 1))); this.orderButton.setMessage(new TextComponent(prefs.terminalItemOrder.name().substring(0, 1)));
this.cancelCraftingButton.visible = this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty(); this.cancelCraftingButton.visible = this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty();
Comparator<ItemStack> comparator = prefs.terminalItemOrder.comparator; var comparator = prefs.terminalItemOrder.comparator;
if (!prefs.terminalAscending) if (!prefs.terminalAscending)
comparator = comparator.reversed(); comparator = comparator.reversed();
// add all items to the sorted items list // add all items to the sorted items list
this.sortedItems.clear(); this.sortedItems.clear();
for (ItemStack stack : this.items) for (var stack : this.items)
this.sortedItems.add(Pair.of(stack, false)); this.sortedItems.add(Pair.of(stack, false));
for (ItemStack stack : this.craftables) for (var stack : this.craftables)
this.sortedItems.add(Pair.of(stack, true)); this.sortedItems.add(Pair.of(stack, true));
// compare by craftability first, and then by the player's chosen order // compare by craftability first, and then by the player's chosen order
Comparator<Pair<ItemStack, Boolean>> fullComparator = Comparator.comparing(Pair::getRight); Comparator<Pair<ItemStack, Boolean>> fullComparator = Comparator.comparing(Pair::getRight);
this.sortedItems.sort(fullComparator.thenComparing(Pair::getLeft, comparator)); this.sortedItems.sort(fullComparator.thenComparing(Pair::getLeft, comparator));
String searchText = this.search.getText(); var searchText = this.search.getValue();
if (!Strings.isNullOrEmpty(searchText)) { if (!Strings.isNullOrEmpty(searchText)) {
this.sortedItems.removeIf(s -> { this.sortedItems.removeIf(s -> {
String search = searchText; var search = searchText;
String toCompare; String toCompare;
if (search.startsWith("@")) { if (search.startsWith("@")) {
toCompare = s.getLeft().getItem().getCreatorModId(s.getLeft()); toCompare = s.getLeft().getItem().getCreatorModId(s.getLeft());
@ -241,16 +240,16 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
if (this.sortedItems.size() < 9 * 4) if (this.sortedItems.size() < 9 * 4)
this.scrollOffset = 0; this.scrollOffset = 0;
List<ItemTerminalWidget> widgets = this.streamWidgets().collect(Collectors.toList()); var widgets = this.streamWidgets().collect(Collectors.toList());
for (int i = 0; i < widgets.size(); i++) { for (var i = 0; i < widgets.size(); i++) {
ItemTerminalWidget widget = widgets.get(i); var widget = widgets.get(i);
int index = i + this.scrollOffset * 9; var index = i + this.scrollOffset * 9;
if (index >= this.sortedItems.size()) { if (index >= this.sortedItems.size()) {
widget.stack = ItemStack.EMPTY; widget.stack = ItemStack.EMPTY;
widget.craftable = false; widget.craftable = false;
widget.visible = false; widget.visible = false;
} else { } else {
Pair<ItemStack, Boolean> stack = this.sortedItems.get(index); var stack = this.sortedItems.get(index);
widget.stack = stack.getLeft(); widget.stack = stack.getLeft();
widget.craftable = stack.getRight(); widget.craftable = stack.getRight();
widget.visible = true; widget.visible = true;
@ -259,69 +258,69 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
} }
@Override @Override
public void render(MatrixStack matrix, int mouseX, int mouseY, float partialTicks) { public void render(PoseStack matrix, int mouseX, int mouseY, float partialTicks) {
this.renderBackground(matrix); this.renderBackground(matrix);
super.render(matrix, mouseX, mouseY, partialTicks); super.render(matrix, mouseX, mouseY, partialTicks);
for (Widget widget : this.buttons) { for (var widget : this.renderables) {
if (widget instanceof ItemTerminalWidget) if (widget instanceof ItemTerminalWidget terminal)
widget.renderToolTip(matrix, mouseX, mouseY); terminal.renderToolTip(matrix, mouseX, mouseY);
} }
if (this.sortedItems != null) { if (this.sortedItems != null) {
PlayerPrefs prefs = PlayerPrefs.get(); var prefs = PlayerPrefs.get();
if (this.orderButton.isHovered()) if (this.orderButton.isHoveredOrFocused())
this.renderTooltip(matrix, new TranslationTextComponent("info." + PrettyPipes.ID + ".order", I18n.format("info." + PrettyPipes.ID + ".order." + prefs.terminalItemOrder.name().toLowerCase(Locale.ROOT))), mouseX, mouseY); this.renderTooltip(matrix, new TranslatableComponent("info." + PrettyPipes.ID + ".order", I18n.get("info." + PrettyPipes.ID + ".order." + prefs.terminalItemOrder.name().toLowerCase(Locale.ROOT))), mouseX, mouseY);
if (this.ascendingButton.isHovered()) if (this.ascendingButton.isHoveredOrFocused())
this.renderTooltip(matrix, new TranslationTextComponent("info." + PrettyPipes.ID + "." + (prefs.terminalAscending ? "ascending" : "descending")), mouseX, mouseY); this.renderTooltip(matrix, new TranslatableComponent("info." + PrettyPipes.ID + "." + (prefs.terminalAscending ? "ascending" : "descending")), mouseX, mouseY);
} }
if (this.cancelCraftingButton.visible && this.cancelCraftingButton.isHovered()) { if (this.cancelCraftingButton.visible && this.cancelCraftingButton.isHoveredOrFocused()) {
String[] tooltip = I18n.format("info." + PrettyPipes.ID + ".cancel_all.desc").split("\n"); var tooltip = I18n.get("info." + PrettyPipes.ID + ".cancel_all.desc").split("\n");
this.func_243308_b(matrix, Arrays.stream(tooltip).map(StringTextComponent::new).collect(Collectors.toList()), mouseX, mouseY); this.renderComponentTooltip(matrix, Arrays.stream(tooltip).map(TextComponent::new).collect(Collectors.toList()), mouseX, mouseY);
} }
if (!this.hoveredCrafting.isEmpty()) if (!this.hoveredCrafting.isEmpty())
this.renderTooltip(matrix, this.hoveredCrafting, mouseX, mouseY); this.renderTooltip(matrix, this.hoveredCrafting, mouseX, mouseY);
this.func_230459_a_(matrix, mouseX, mouseY); this.renderTooltip(matrix, mouseX, mouseY);
} }
@Override @Override
protected void drawGuiContainerForegroundLayer(MatrixStack matrix, int mouseX, int mouseY) { protected void renderLabels(PoseStack matrix, int mouseX, int mouseY) {
this.font.drawString(matrix, this.playerInventory.getDisplayName().getString(), 8 + this.getXOffset(), this.ySize - 96 + 2, 4210752); this.font.draw(matrix, this.playerInventoryTitle.getString(), 8 + this.getXOffset(), this.imageHeight - 96 + 2, 4210752);
this.font.drawString(matrix, this.title.getString(), 8, 6, 4210752); this.font.draw(matrix, this.title.getString(), 8, 6, 4210752);
String amount = String.valueOf(this.requestAmount); var amount = String.valueOf(this.requestAmount);
this.font.drawString(matrix, amount, (176 + 15 - this.font.getStringWidth(amount)) / 2F - 7 + this.getXOffset(), 106, 4210752); this.font.draw(matrix, amount, (176 + 15 - this.font.width(amount)) / 2F - 7 + this.getXOffset(), 106, 4210752);
if (this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty()) { if (this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty()) {
this.font.drawString(matrix, I18n.format("info." + PrettyPipes.ID + ".crafting"), this.xSize + 4, 4 + 6, 4210752); this.font.draw(matrix, I18n.get("info." + PrettyPipes.ID + ".crafting"), this.imageWidth + 4, 4 + 6, 4210752);
if (this.currentlyCrafting.size() > 6) if (this.currentlyCrafting.size() > 6)
this.font.drawString(matrix, ". . .", this.xSize + 24, 4 + 51, 4210752); this.font.draw(matrix, ". . .", this.imageWidth + 24, 4 + 51, 4210752);
} }
} }
@Override @Override
protected void drawGuiContainerBackgroundLayer(MatrixStack matrix, float partialTicks, int mouseX, int mouseY) { protected void renderBg(PoseStack matrix, float partialTicks, int mouseX, int mouseY) {
this.getMinecraft().getTextureManager().bindTexture(this.getTexture()); this.getMinecraft().getTextureManager().bindForSetup(this.getTexture());
this.blit(matrix, this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize); this.blit(matrix, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight);
if (this.sortedItems != null && this.sortedItems.size() >= 9 * 4) { if (this.sortedItems != null && this.sortedItems.size() >= 9 * 4) {
float percentage = this.scrollOffset / (float) (this.sortedItems.size() / 9 - 3); var percentage = this.scrollOffset / (float) (this.sortedItems.size() / 9 - 3);
this.blit(matrix, this.guiLeft + this.getXOffset() + 172, this.guiTop + 18 + (int) (percentage * (70 - 15)), 232, 241, 12, 15); this.blit(matrix, this.leftPos + this.getXOffset() + 172, this.topPos + 18 + (int) (percentage * (70 - 15)), 232, 241, 12, 15);
} else { } else {
this.blit(matrix, this.guiLeft + this.getXOffset() + 172, this.guiTop + 18, 244, 241, 12, 15); this.blit(matrix, this.leftPos + this.getXOffset() + 172, this.topPos + 18, 244, 241, 12, 15);
} }
// draw the items that are currently crafting // draw the items that are currently crafting
this.hoveredCrafting = ItemStack.EMPTY; this.hoveredCrafting = ItemStack.EMPTY;
if (this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty()) { if (this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty()) {
this.getMinecraft().getTextureManager().bindTexture(TEXTURE); this.getMinecraft().getTextureManager().bindForSetup(TEXTURE);
this.blit(matrix, this.guiLeft + this.xSize, this.guiTop + 4, 191, 0, 65, 89); this.blit(matrix, this.leftPos + this.imageWidth, this.topPos + 4, 191, 0, 65, 89);
int x = 0; var x = 0;
int y = 0; var y = 0;
for (ItemStack stack : this.currentlyCrafting) { for (var stack : this.currentlyCrafting) {
int itemX = this.guiLeft + this.xSize + 4 + x * 18; var itemX = this.leftPos + this.imageWidth + 4 + x * 18;
int itemY = this.guiTop + 4 + 16 + y * 18; var itemY = this.topPos + 4 + 16 + y * 18;
this.itemRenderer.renderItemAndEffectIntoGUI(stack, itemX, itemY); this.itemRenderer.renderGuiItem(stack, itemX, itemY);
this.itemRenderer.renderItemOverlayIntoGUI(this.font, stack, itemX, itemY, String.valueOf(stack.getCount())); this.itemRenderer.renderGuiItemDecorations(this.font, stack, itemX, itemY, String.valueOf(stack.getCount()));
if (mouseX >= itemX && mouseY >= itemY && mouseX < itemX + 16 && mouseY < itemY + 18) if (mouseX >= itemX && mouseY >= itemY && mouseX < itemX + 16 && mouseY < itemY + 18)
this.hoveredCrafting = stack; this.hoveredCrafting = stack;
x++; x++;
@ -342,7 +341,7 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
@Override @Override
public boolean mouseScrolled(double x, double y, double scroll) { public boolean mouseScrolled(double x, double y, double scroll) {
if (this.sortedItems != null && this.sortedItems.size() >= 9 * 4) { if (this.sortedItems != null && this.sortedItems.size() >= 9 * 4) {
int offset = MathHelper.clamp(this.scrollOffset - (int) Math.signum(scroll), 0, this.sortedItems.size() / 9 - 3); var offset = Mth.clamp(this.scrollOffset - (int) Math.signum(scroll), 0, this.sortedItems.size() / 9 - 3);
if (offset != this.scrollOffset) { if (offset != this.scrollOffset) {
this.scrollOffset = offset; this.scrollOffset = offset;
this.updateWidgets(); this.updateWidgets();
@ -352,7 +351,7 @@ public class ItemTerminalGui extends ContainerScreen<ItemTerminalContainer> {
} }
public Stream<ItemTerminalWidget> streamWidgets() { public Stream<ItemTerminalWidget> streamWidgets() {
return this.buttons.stream() return this.renderables.stream()
.filter(w -> w instanceof ItemTerminalWidget) .filter(w -> w instanceof ItemTerminalWidget)
.map(w -> (ItemTerminalWidget) w); .map(w -> (ItemTerminalWidget) w);
} }

View file

@ -1,32 +1,30 @@
package de.ellpeck.prettypipes.terminal.containers; package de.ellpeck.prettypipes.terminal.containers;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.world.entity.player.StackedContents;
import net.minecraft.inventory.CraftingInventory; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.inventory.ItemStackHelper; import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.inventory.container.Container;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.item.crafting.RecipeItemHelper;
import net.minecraftforge.items.ItemStackHandler; import net.minecraftforge.items.ItemStackHandler;
public class WrappedCraftingInventory extends CraftingInventory { public class WrappedCraftingInventory extends CraftingContainer {
private final ItemStackHandler items; private final ItemStackHandler items;
private final Container eventHandler; private final AbstractContainerMenu eventHandler;
public WrappedCraftingInventory(ItemStackHandler items, Container eventHandlerIn, int width, int height) { public WrappedCraftingInventory(ItemStackHandler items, AbstractContainerMenu eventHandlerIn, int width, int height) {
super(eventHandlerIn, width, height); super(eventHandlerIn, width, height);
this.eventHandler = eventHandlerIn; this.eventHandler = eventHandlerIn;
this.items = items; this.items = items;
} }
@Override @Override
public int getSizeInventory() { public int getContainerSize() {
return this.items.getSlots(); return this.items.getSlots();
} }
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
for (int i = 0; i < this.items.getSlots(); i++) { for (var i = 0; i < this.items.getSlots(); i++) {
if (!this.items.getStackInSlot(i).isEmpty()) if (!this.items.getStackInSlot(i).isEmpty())
return false; return false;
} }
@ -34,41 +32,41 @@ public class WrappedCraftingInventory extends CraftingInventory {
} }
@Override @Override
public ItemStack getStackInSlot(int index) { public ItemStack getItem(int index) {
return this.items.getStackInSlot(index); return this.items.getStackInSlot(index);
} }
@Override @Override
public ItemStack removeStackFromSlot(int index) { public ItemStack removeItemNoUpdate(int index) {
ItemStack before = this.items.getStackInSlot(index); var before = this.items.getStackInSlot(index);
this.items.setStackInSlot(index, ItemStack.EMPTY); this.items.setStackInSlot(index, ItemStack.EMPTY);
return before; return before;
} }
@Override @Override
public ItemStack decrStackSize(int index, int count) { public ItemStack removeItem(int index, int count) {
ItemStack slotStack = this.items.getStackInSlot(index); var slotStack = this.items.getStackInSlot(index);
ItemStack ret = !slotStack.isEmpty() && count > 0 ? slotStack.split(count) : ItemStack.EMPTY; var ret = !slotStack.isEmpty() && count > 0 ? slotStack.split(count) : ItemStack.EMPTY;
if (!ret.isEmpty()) if (!ret.isEmpty())
this.eventHandler.onCraftMatrixChanged(this); this.eventHandler.slotsChanged(this);
return ret; return ret;
} }
@Override @Override
public void setInventorySlotContents(int index, ItemStack stack) { public void setItem(int index, ItemStack stack) {
this.items.setStackInSlot(index, stack); this.items.setStackInSlot(index, stack);
this.eventHandler.onCraftMatrixChanged(this); this.eventHandler.slotsChanged(this);
} }
@Override @Override
public void clear() { public void clearContent() {
for (int i = 0; i < this.items.getSlots(); i++) for (var i = 0; i < this.items.getSlots(); i++)
this.items.setStackInSlot(i, ItemStack.EMPTY); this.items.setStackInSlot(i, ItemStack.EMPTY);
} }
@Override @Override
public void fillStackedContents(RecipeItemHelper helper) { public void fillStackedContents(StackedContents helper) {
for (int i = 0; i < this.items.getSlots(); i++) for (var i = 0; i < this.items.getSlots(); i++)
helper.accountPlainStack(this.items.getStackInSlot(i)); helper.accountStack(this.items.getStackInSlot(i));
} }
} }