ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItemAdvanced.java

153 lines
5.7 KiB
Java
Raw Normal View History

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
*/
package de.ellpeck.actuallyadditions.mod.tile;
2016-06-07 23:17:06 +02:00
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFilter;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerLaserRelayItemWhitelist;
import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotFilter;
2016-06-07 23:17:06 +02:00
import de.ellpeck.actuallyadditions.mod.items.ItemDrill;
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import de.ellpeck.actuallyadditions.mod.util.compat.SlotlessableItemHandlerWrapper;
2021-02-26 22:15:48 +01:00
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.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import javax.annotation.Nullable;
public class TileEntityLaserRelayItemAdvanced extends TileEntityLaserRelayItem implements IButtonReactor, INamedContainerProvider {
public FilterSettings leftFilter = new FilterSettings(12, true, true, false, false, 0, -1000);
public FilterSettings rightFilter = new FilterSettings(12, true, true, false, false, 0, -2000);
public TileEntityLaserRelayItemAdvanced() {
2021-02-27 16:33:00 +01:00
super(ActuallyTiles.LASERRELAYITEMWHITELIST_TILE.get());
}
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
2021-02-26 22:15:48 +01:00
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
super.writeSyncableNBT(compound, type);
this.leftFilter.writeToNBT(compound, "LeftFilter");
this.rightFilter.writeToNBT(compound, "RightFilter");
2016-06-17 23:50:38 +02:00
}
@Override
2021-02-26 22:15:48 +01:00
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
super.readSyncableNBT(compound, type);
this.leftFilter.readFromNBT(compound, "LeftFilter");
this.rightFilter.readFromNBT(compound, "RightFilter");
}
@Override
2021-02-26 22:15:48 +01:00
public void onButtonPressed(int buttonID, PlayerEntity player) {
this.leftFilter.onButtonPressed(buttonID);
this.rightFilter.onButtonPressed(buttonID);
2019-05-02 09:10:29 +02:00
if (buttonID == 2) {
this.addWhitelistSmart(false);
2019-05-02 09:10:29 +02:00
} else if (buttonID == 3) {
this.addWhitelistSmart(true);
}
}
2019-05-02 09:10:29 +02:00
private void addWhitelistSmart(boolean output) {
for (SlotlessableItemHandlerWrapper handler : this.handlersAround.values()) {
handler.getNormalHandler().ifPresent(itemHandler -> {
2019-05-02 09:10:29 +02:00
for (int i = 0; i < itemHandler.getSlots(); i++) {
ItemStack stack = itemHandler.getStackInSlot(i);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(stack)) {
this.addWhitelistSmart(output, stack);
}
}
});
}
}
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;
ItemStack copy = stack.copy();
copy.setCount(1);
2019-05-02 09:10:29 +02:00
if (!FilterSettings.check(copy, usedSettings.filterInventory, true, usedSettings.respectMeta, usedSettings.respectNBT, usedSettings.respectMod, usedSettings.respectOredict)) {
for (int k = 0; k < usedSettings.filterInventory.getSlots(); k++) {
ItemStack slot = usedSettings.filterInventory.getStackInSlot(k);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(slot)) {
if (SlotFilter.isFilter(slot)) {
ItemStackHandlerAA inv = new ItemStackHandlerAA(ContainerFilter.SLOT_AMOUNT);
ItemDrill.loadSlotsFromNBT(inv, slot);
boolean did = false;
2019-05-02 09:10:29 +02:00
for (int j = 0; j < inv.getSlots(); j++) {
if (!StackUtil.isValid(inv.getStackInSlot(j))) {
inv.setStackInSlot(j, copy);
did = true;
break;
}
2016-06-07 23:17:06 +02:00
}
2019-05-02 09:10:29 +02:00
if (did) {
ItemDrill.writeSlotsToNBT(inv, slot);
break;
}
}
2019-05-02 09:10:29 +02:00
} else {
usedSettings.filterInventory.setStackInSlot(k, copy);
break;
}
}
}
}
@Override
2019-05-02 09:10:29 +02:00
public void updateEntity() {
super.updateEntity();
2019-05-02 09:10:29 +02:00
if (!this.world.isRemote) {
if ((this.leftFilter.needsUpdateSend() || this.rightFilter.needsUpdateSend()) && this.sendUpdateWithInterval()) {
this.leftFilter.updateLasts();
this.rightFilter.updateLasts();
}
}
}
@Override
public ITextComponent getDisplayName() {
return StringTextComponent.EMPTY;
}
@Nullable
@Override
public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity player) {
return new ContainerLaserRelayItemWhitelist(windowId, playerInventory, this);
}
}