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

118 lines
4.2 KiB
Java
Raw Normal View History

2015-10-10 02:51:06 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityRangedCollector.java") is part of the Actually Additions mod for Minecraft.
2015-10-10 02:51:06 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-10-10 02:51:06 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-10-10 02:51:06 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-10-10 02:51:06 +02:00
import de.ellpeck.actuallyadditions.mod.inventory.ContainerRangedCollector;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2021-02-27 16:33:00 +01:00
import net.minecraft.entity.item.ItemEntity;
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;
2015-10-10 02:51:06 +02:00
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
2021-02-27 16:33:00 +01:00
import net.minecraft.particles.ParticleTypes;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
2021-02-27 16:33:00 +01:00
import net.minecraft.world.server.ServerWorld;
2015-10-10 02:51:06 +02:00
import javax.annotation.Nullable;
2021-02-26 22:15:48 +01:00
import java.util.ArrayList;
import java.util.List;
public class TileEntityRangedCollector extends TileEntityInventoryBase implements IButtonReactor, INamedContainerProvider {
2015-10-10 02:51:06 +02:00
2015-12-01 19:48:09 +01:00
public static final int RANGE = 6;
public FilterSettings filter = new FilterSettings(12, true, true, false, false, 0, -1000);
2015-10-10 02:51:06 +02:00
public TileEntityRangedCollector() {
2021-02-27 16:33:00 +01:00
super(ActuallyTiles.RANGEDCOLLECTOR_TILE.get(), 6);
2015-10-10 02:51:06 +02:00
}
2016-02-01 17:49:55 +01:00
@Override
2021-02-26 22:15:48 +01:00
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
super.writeSyncableNBT(compound, type);
this.filter.writeToNBT(compound, "Filter");
2016-02-01 17:49:55 +01:00
}
@Override
2021-02-26 22:15:48 +01:00
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
super.readSyncableNBT(compound, type);
this.filter.readFromNBT(compound, "Filter");
2016-02-01 17:49:55 +01:00
}
2015-10-10 02:51:06 +02:00
@Override
public boolean isRedstoneToggle() {
return true;
}
@Override
public void activateOnPulse() {
2021-02-27 16:33:00 +01:00
List<ItemEntity> items = this.world.getEntitiesWithinAABB(ItemEntity.class, new AxisAlignedBB(this.pos.getX() - RANGE, this.pos.getY() - RANGE, this.pos.getZ() - RANGE, this.pos.getX() + RANGE, this.pos.getY() + RANGE, this.pos.getZ() + RANGE));
if (!items.isEmpty()) {
2021-02-27 16:33:00 +01:00
for (ItemEntity item : items) {
if (item.isAlive() && !item.cannotPickup() && StackUtil.isValid(item.getItem())) {
ItemStack toAdd = item.getItem().copy();
if (this.filter.check(toAdd)) {
2019-02-27 19:53:05 +01:00
ArrayList<ItemStack> checkList = new ArrayList<>();
checkList.add(toAdd);
2019-02-27 19:53:05 +01:00
if (StackUtil.canAddAll(this.inv, checkList, false)) {
StackUtil.addAll(this.inv, checkList, false);
2021-02-27 16:33:00 +01:00
((ServerWorld) this.world).spawnParticle(ParticleTypes.CLOUD, item.getPosX(), item.getPosY() + 0.45F, item.getPosZ(), 5, 0, 0, 0, 0.03D);
item.remove();
2015-10-10 02:51:06 +02:00
}
}
}
}
}
}
@Override
public void updateEntity() {
super.updateEntity();
if (!this.world.isRemote) {
if (!this.isRedstonePowered && !this.isPulseMode) {
this.activateOnPulse();
}
2015-10-10 02:51:06 +02:00
if (this.filter.needsUpdateSend() && this.sendUpdateWithInterval()) {
this.filter.updateLasts();
2015-10-10 02:51:06 +02:00
}
}
}
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
return (slot, stack, automation) -> !automation;
2015-10-10 02:51:06 +02:00
}
@Override
2021-02-26 22:15:48 +01:00
public void onButtonPressed(int buttonID, PlayerEntity player) {
this.filter.onButtonPressed(buttonID);
2015-10-10 02:51:06 +02:00
}
@Override
public ITextComponent getDisplayName() {
return StringTextComponent.EMPTY;
}
@Nullable
@Override
public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity player) {
return new ContainerRangedCollector(windowId, playerInventory, this);
}
2015-10-10 02:51:06 +02:00
}