2016-05-16 22:52:27 +02:00
|
|
|
/*
|
|
|
|
* This file ("TileEntityLaserRelayItemWhitelist.java") is part of the Actually Additions mod for Minecraft.
|
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
|
|
|
* http://ellpeck.de/actaddlicense
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2016-05-16 22:52:27 +02:00
|
|
|
*/
|
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2021-11-13 18:16:25 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2016-06-07 23:17:06 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFilter;
|
2021-03-01 18:46:12 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerLaserRelayItemWhitelist;
|
2016-12-03 10:52:03 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotFilter;
|
2021-11-25 21:27:45 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.DrillItem;
|
2016-05-14 17:50:10 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
|
2018-06-23 01:39:30 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
|
2017-02-04 14:12:17 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.compat.SlotlessableItemHandlerWrapper;
|
2024-03-02 21:23:08 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.network.chat.Component;
|
|
|
|
import net.minecraft.world.MenuProvider;
|
|
|
|
import net.minecraft.world.entity.player.Inventory;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
|
|
import net.minecraft.world.inventory.AbstractContainerMenu;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2021-03-01 18:46:12 +01:00
|
|
|
import javax.annotation.Nullable;
|
2024-03-04 21:38:02 +01:00
|
|
|
import java.util.Optional;
|
2021-03-01 18:46:12 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public class TileEntityLaserRelayItemAdvanced extends TileEntityLaserRelayItem implements IButtonReactor, MenuProvider {
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2024-03-05 23:23:49 +01:00
|
|
|
public FilterSettings leftFilter = new FilterSettings(12, true, false);
|
|
|
|
public FilterSettings rightFilter = new FilterSettings(12, true, false);
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public TileEntityLaserRelayItemAdvanced(BlockPos pos, BlockState state) {
|
|
|
|
super(ActuallyBlocks.LASER_RELAY_ITEM_ADVANCED.getTileEntityType(), pos, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
|
|
|
|
if (t instanceof TileEntityLaserRelayItemAdvanced tile) {
|
|
|
|
tile.clientTick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
|
|
|
|
if (t instanceof TileEntityLaserRelayItemAdvanced tile) {
|
|
|
|
tile.serverTick();
|
|
|
|
|
|
|
|
if ((tile.leftFilter.needsUpdateSend() || tile.rightFilter.needsUpdateSend()) && tile.sendUpdateWithInterval()) {
|
|
|
|
tile.leftFilter.updateLasts();
|
|
|
|
tile.rightFilter.updateLasts();
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
2016-11-16 18:51:23 +01:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getPriority() {
|
|
|
|
return super.getPriority() + 10;
|
2016-11-16 18:51:23 +01:00
|
|
|
}
|
|
|
|
|
2016-06-17 23:50:38 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean isWhitelisted(ItemStack stack, boolean output) {
|
2021-02-26 22:15:48 +01:00
|
|
|
return output
|
|
|
|
? this.rightFilter.check(stack)
|
|
|
|
: this.leftFilter.check(stack);
|
2016-06-17 23:50:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2016-12-03 10:52:03 +01:00
|
|
|
|
|
|
|
this.leftFilter.writeToNBT(compound, "LeftFilter");
|
|
|
|
this.rightFilter.writeToNBT(compound, "RightFilter");
|
2016-06-17 23:50:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void readSyncableNBT(CompoundTag compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2016-12-03 10:52:03 +01:00
|
|
|
|
|
|
|
this.leftFilter.readFromNBT(compound, "LeftFilter");
|
|
|
|
this.rightFilter.readFromNBT(compound, "RightFilter");
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void onButtonPressed(int buttonID, Player player) {
|
2016-07-23 19:20:53 +02:00
|
|
|
this.leftFilter.onButtonPressed(buttonID);
|
|
|
|
this.rightFilter.onButtonPressed(buttonID);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (buttonID == 2) {
|
2016-06-06 17:59:01 +02:00
|
|
|
this.addWhitelistSmart(false);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else if (buttonID == 3) {
|
2016-06-06 17:59:01 +02:00
|
|
|
this.addWhitelistSmart(true);
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
private void addWhitelistSmart(boolean output) {
|
|
|
|
for (SlotlessableItemHandlerWrapper handler : this.handlersAround.values()) {
|
2024-03-04 21:38:02 +01:00
|
|
|
Optional.ofNullable(handler.getNormalHandler()).ifPresent(itemHandler -> {
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int i = 0; i < itemHandler.getSlots(); i++) {
|
2017-02-04 14:12:17 +01:00
|
|
|
ItemStack stack = itemHandler.getStackInSlot(i);
|
2023-03-16 00:17:57 +01:00
|
|
|
if (!stack.isEmpty()) {
|
2017-02-04 14:12:17 +01:00
|
|
|
this.addWhitelistSmart(output, stack);
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 18:46:12 +01:00
|
|
|
});
|
2017-02-04 14:12:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
private void addWhitelistSmart(boolean output, ItemStack stack) {
|
2021-02-26 22:15:48 +01:00
|
|
|
FilterSettings usedSettings = output
|
|
|
|
? this.rightFilter
|
|
|
|
: this.leftFilter;
|
2017-02-04 14:12:17 +01:00
|
|
|
ItemStack copy = stack.copy();
|
2018-06-23 01:39:30 +02:00
|
|
|
copy.setCount(1);
|
2017-02-04 14:12:17 +01:00
|
|
|
|
2024-03-05 23:23:49 +01:00
|
|
|
if (!FilterSettings.check(copy, usedSettings.filterInventory, true, usedSettings.respectMod)) {
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int k = 0; k < usedSettings.filterInventory.getSlots(); k++) {
|
2017-02-04 14:12:17 +01:00
|
|
|
ItemStack slot = usedSettings.filterInventory.getStackInSlot(k);
|
2023-03-16 00:17:57 +01:00
|
|
|
if (!slot.isEmpty()) {
|
2019-05-02 09:10:29 +02:00
|
|
|
if (SlotFilter.isFilter(slot)) {
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStackHandlerAA inv = new ItemStackHandlerAA(ContainerFilter.SLOT_AMOUNT);
|
2021-11-25 21:27:45 +01:00
|
|
|
DrillItem.loadSlotsFromNBT(inv, slot);
|
2017-02-04 14:12:17 +01:00
|
|
|
|
|
|
|
boolean did = false;
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int j = 0; j < inv.getSlots(); j++) {
|
2023-03-16 00:17:57 +01:00
|
|
|
if (inv.getStackInSlot(j).isEmpty()) {
|
2017-02-04 14:12:17 +01:00
|
|
|
inv.setStackInSlot(j, copy);
|
|
|
|
did = true;
|
2016-06-10 17:53:11 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-06-07 23:17:06 +02:00
|
|
|
}
|
2017-02-04 14:12:17 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (did) {
|
2021-11-25 21:27:45 +01:00
|
|
|
DrillItem.writeSlotsToNBT(inv, slot);
|
2017-02-04 14:12:17 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2017-02-04 14:12:17 +01:00
|
|
|
usedSettings.filterInventory.setStackInSlot(k, copy);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public Component getDisplayName() {
|
2024-03-12 21:31:29 +01:00
|
|
|
return Component.translatable("container.actuallyadditions.laserRelayAdvanced");
|
2021-03-01 18:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player player) {
|
2021-03-01 18:46:12 +01:00
|
|
|
return new ContainerLaserRelayItemWhitelist(windowId, playerInventory, this);
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|