Dynamic portable grid model
This commit is contained in:
@@ -4,6 +4,7 @@ import com.raoulvdberge.refinedstorage.RS;
|
||||
import com.raoulvdberge.refinedstorage.RSGui;
|
||||
import com.raoulvdberge.refinedstorage.item.ItemBlockPortableGrid;
|
||||
import com.raoulvdberge.refinedstorage.tile.grid.portable.TilePortableGrid;
|
||||
import net.minecraft.block.properties.PropertyBool;
|
||||
import net.minecraft.block.properties.PropertyEnum;
|
||||
import net.minecraft.block.state.BlockStateContainer;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
@@ -27,6 +28,8 @@ public class BlockPortableGrid extends BlockBase {
|
||||
private static final AxisAlignedBB PORTABLE_GRID_AABB = new AxisAlignedBB(0, 0, 0, 1, 13.2F / 16F, 1);
|
||||
|
||||
public static final PropertyEnum TYPE = PropertyEnum.create("type", PortableGridType.class);
|
||||
public static final PropertyEnum DISK_STATE = PropertyEnum.create("disk_state", PortableGridDiskState.class);
|
||||
public static final PropertyBool CONNECTED = PropertyBool.create("connected");
|
||||
|
||||
public BlockPortableGrid() {
|
||||
super("portable_grid");
|
||||
@@ -89,10 +92,21 @@ public class BlockPortableGrid extends BlockBase {
|
||||
return drops;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos) {
|
||||
TilePortableGrid portableGrid = (TilePortableGrid) world.getTileEntity(pos);
|
||||
|
||||
return super.getActualState(state, world, pos)
|
||||
.withProperty(DISK_STATE, portableGrid.getDiskState())
|
||||
.withProperty(CONNECTED, portableGrid.isConnected());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BlockStateContainer createBlockState() {
|
||||
return createBlockStateBuilder()
|
||||
.add(TYPE)
|
||||
.add(DISK_STATE)
|
||||
.add(CONNECTED)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.raoulvdberge.refinedstorage.block;
|
||||
|
||||
import net.minecraft.util.IStringSerializable;
|
||||
|
||||
public enum PortableGridDiskState implements IStringSerializable {
|
||||
NORMAL(0, "normal"),
|
||||
NEAR_CAPACITY(1, "near_capacity"),
|
||||
FULL(2, "full"),
|
||||
DISCONNECTED(3, "disconnected"),
|
||||
NONE(4, "none");
|
||||
|
||||
private int id;
|
||||
private String type;
|
||||
|
||||
PortableGridDiskState(int id, String type) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public static PortableGridDiskState getById(int id) {
|
||||
for (PortableGridDiskState diskState : values()) {
|
||||
if (diskState.getId() == id) {
|
||||
return diskState;
|
||||
}
|
||||
}
|
||||
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ import com.raoulvdberge.refinedstorage.render.ModelDiskManipulator;
|
||||
import com.raoulvdberge.refinedstorage.render.TileEntitySpecialRendererStorageMonitor;
|
||||
import com.raoulvdberge.refinedstorage.tile.TileController;
|
||||
import com.raoulvdberge.refinedstorage.tile.TileStorageMonitor;
|
||||
import com.raoulvdberge.refinedstorage.tile.grid.portable.PortableGrid;
|
||||
import com.raoulvdberge.refinedstorage.tile.grid.portable.TilePortableGrid;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.client.Minecraft;
|
||||
@@ -211,7 +213,6 @@ public class ProxyClient extends ProxyCommon {
|
||||
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(RSBlocks.SECURITY_MANAGER), 0, new ModelResourceLocation("refinedstorage:security_manager", "inventory"));
|
||||
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(RSBlocks.QUARTZ_ENRICHED_IRON), 0, new ModelResourceLocation("refinedstorage:quartz_enriched_iron_block", "inventory"));
|
||||
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(RSBlocks.STORAGE_MONITOR), 0, new ModelResourceLocation("refinedstorage:storage_monitor", "connected=false,direction=north"));
|
||||
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(RSBlocks.PORTABLE_GRID), 0, new ModelResourceLocation("refinedstorage:portable_grid", "inventory"));
|
||||
|
||||
ModelLoaderRegistry.registerLoader(new ICustomModelLoader() {
|
||||
@Override
|
||||
@@ -253,6 +254,12 @@ public class ProxyClient extends ProxyCommon {
|
||||
|
||||
return new ModelResourceLocation("refinedstorage:controller", "direction=north,energy=" + energy);
|
||||
});
|
||||
|
||||
ModelLoader.setCustomMeshDefinition(Item.getItemFromBlock(RSBlocks.PORTABLE_GRID), stack -> {
|
||||
PortableGrid portableGrid = new PortableGrid(null, stack);
|
||||
|
||||
return new ModelResourceLocation("refinedstorage:portable_grid", "connected=" + Boolean.toString(portableGrid.getEnergy() != 0) + ",direction=north,disk_state=" + TilePortableGrid.getDiskState(portableGrid));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,22 +1,26 @@
|
||||
package com.raoulvdberge.refinedstorage.tile.grid.portable;
|
||||
|
||||
import com.raoulvdberge.refinedstorage.api.storage.IStorage;
|
||||
import com.raoulvdberge.refinedstorage.api.storage.IStorageCache;
|
||||
import com.raoulvdberge.refinedstorage.api.storage.IStorageDisk;
|
||||
import com.raoulvdberge.refinedstorage.inventory.ItemHandlerBase;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
|
||||
public interface IPortableGrid {
|
||||
IStorageCache<ItemStack> getCache();
|
||||
|
||||
IStorage<ItemStack> getStorage();
|
||||
@Nullable
|
||||
IStorageDisk<ItemStack> getStorage();
|
||||
|
||||
List<EntityPlayer> getWatchers();
|
||||
|
||||
void drainEnergy(int energy);
|
||||
|
||||
int getEnergy();
|
||||
|
||||
ItemHandlerBase getDisk();
|
||||
|
||||
ItemHandlerBase getFilter();
|
||||
|
||||
@@ -17,6 +17,7 @@ import com.raoulvdberge.refinedstorage.gui.grid.GuiGrid;
|
||||
import com.raoulvdberge.refinedstorage.inventory.ItemHandlerBase;
|
||||
import com.raoulvdberge.refinedstorage.inventory.ItemHandlerFilter;
|
||||
import com.raoulvdberge.refinedstorage.item.ItemBlockPortableGrid;
|
||||
import com.raoulvdberge.refinedstorage.item.ItemEnergyItem;
|
||||
import com.raoulvdberge.refinedstorage.item.ItemWirelessGrid;
|
||||
import com.raoulvdberge.refinedstorage.item.filter.Filter;
|
||||
import com.raoulvdberge.refinedstorage.item.filter.FilterTab;
|
||||
@@ -82,14 +83,19 @@ public class PortableGrid implements IGrid, IPortableGrid {
|
||||
IStorageDiskProvider provider = (IStorageDiskProvider) getStackInSlot(slot).getItem();
|
||||
|
||||
storage = new StorageDiskItemPortable(provider.create(getStackInSlot(slot)), PortableGrid.this);
|
||||
storage.readFromNBT();
|
||||
storage.onPassContainerContext(() -> {
|
||||
}, () -> false, () -> AccessType.INSERT_EXTRACT);
|
||||
|
||||
if (player != null) {
|
||||
storage.readFromNBT();
|
||||
storage.onPassContainerContext(() -> {
|
||||
}, () -> false, () -> AccessType.INSERT_EXTRACT);
|
||||
}
|
||||
}
|
||||
|
||||
cache.invalidate();
|
||||
if (player != null) {
|
||||
cache.invalidate();
|
||||
|
||||
RSUtils.writeItems(this, 4, stack.getTagCompound());
|
||||
RSUtils.writeItems(this, 4, stack.getTagCompound());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,31 +110,37 @@ public class PortableGrid implements IGrid, IPortableGrid {
|
||||
}
|
||||
};
|
||||
|
||||
public PortableGrid(EntityPlayer player, ItemStack stack) {
|
||||
public PortableGrid(@Nullable EntityPlayer player, ItemStack stack) {
|
||||
this.player = player;
|
||||
this.stack = stack;
|
||||
|
||||
this.sortingType = ItemWirelessGrid.getSortingType(stack);
|
||||
this.sortingDirection = ItemWirelessGrid.getSortingDirection(stack);
|
||||
this.searchBoxMode = ItemWirelessGrid.getSearchBoxMode(stack);
|
||||
this.tabSelected = ItemWirelessGrid.getTabSelected(stack);
|
||||
this.size = ItemWirelessGrid.getSize(stack);
|
||||
if (player != null) {
|
||||
this.sortingType = ItemWirelessGrid.getSortingType(stack);
|
||||
this.sortingDirection = ItemWirelessGrid.getSortingDirection(stack);
|
||||
this.searchBoxMode = ItemWirelessGrid.getSearchBoxMode(stack);
|
||||
this.tabSelected = ItemWirelessGrid.getTabSelected(stack);
|
||||
this.size = ItemWirelessGrid.getSize(stack);
|
||||
}
|
||||
|
||||
if (!stack.hasTagCompound()) {
|
||||
stack.setTagCompound(new NBTTagCompound());
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
RSUtils.readItems(filter, i, stack.getTagCompound());
|
||||
if (player != null) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
RSUtils.readItems(filter, i, stack.getTagCompound());
|
||||
}
|
||||
}
|
||||
|
||||
RSUtils.readItems(disk, 4, stack.getTagCompound());
|
||||
|
||||
drainEnergy(RS.INSTANCE.config.portableGridOpenUsage);
|
||||
if (player != null) {
|
||||
drainEnergy(RS.INSTANCE.config.portableGridOpenUsage);
|
||||
|
||||
// If there is no disk onContentsChanged isn't called and the update isn't sent, thus items from the previous grid view would remain clientside
|
||||
if (!player.getEntityWorld().isRemote && disk.getStackInSlot(0).isEmpty()) {
|
||||
cache.invalidate();
|
||||
// If there is no disk onContentsChanged isn't called and the update isn't sent, thus items from the previous grid view would remain clientside
|
||||
if (!player.getEntityWorld().isRemote && disk.getStackInSlot(0).isEmpty()) {
|
||||
cache.invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,10 +148,12 @@ public class PortableGrid implements IGrid, IPortableGrid {
|
||||
return stack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageCacheItemPortable getCache() {
|
||||
return cache;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public IStorageDisk<ItemStack> getStorage() {
|
||||
return storage;
|
||||
@@ -157,6 +171,16 @@ public class PortableGrid implements IGrid, IPortableGrid {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEnergy() {
|
||||
if (RS.INSTANCE.config.portableGridUsesEnergy && stack.getItemDamage() != ItemBlockPortableGrid.TYPE_CREATIVE) {
|
||||
return stack.getCapability(CapabilityEnergy.ENERGY, null).getEnergyStored();
|
||||
}
|
||||
|
||||
return ItemEnergyItem.CAPACITY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemHandlerBase getDisk() {
|
||||
return disk;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import com.raoulvdberge.refinedstorage.apiimpl.storage.StorageCacheItemPortable;
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.storage.StorageDiskItemPortable;
|
||||
import com.raoulvdberge.refinedstorage.block.BlockPortableGrid;
|
||||
import com.raoulvdberge.refinedstorage.block.GridType;
|
||||
import com.raoulvdberge.refinedstorage.block.PortableGridDiskState;
|
||||
import com.raoulvdberge.refinedstorage.block.PortableGridType;
|
||||
import com.raoulvdberge.refinedstorage.gui.grid.GuiGrid;
|
||||
import com.raoulvdberge.refinedstorage.integration.forgeenergy.EnergyForge;
|
||||
@@ -146,6 +147,8 @@ public class TilePortableGrid extends TileBase implements IGrid, IPortableGrid,
|
||||
public static final TileDataParameter<Integer> REDSTONE_MODE = RedstoneMode.createParameter();
|
||||
|
||||
private static final String NBT_ENERGY = "Energy";
|
||||
private static final String NBT_DISK_STATE = "DiskState";
|
||||
private static final String NBT_CONNECTED = "Connected";
|
||||
|
||||
private EnergyForge energyStorage = new EnergyForge(ItemEnergyItem.CAPACITY);
|
||||
private PortableGridType type;
|
||||
@@ -174,10 +177,15 @@ public class TilePortableGrid extends TileBase implements IGrid, IPortableGrid,
|
||||
|
||||
storage = new StorageDiskItemPortable(provider.create(getStackInSlot(slot)), TilePortableGrid.this);
|
||||
storage.readFromNBT();
|
||||
storage.onPassContainerContext(TilePortableGrid.this::markDirty, () -> false, () -> AccessType.INSERT_EXTRACT);
|
||||
storage.onPassContainerContext(() -> {
|
||||
TilePortableGrid.this.markDirty();
|
||||
TilePortableGrid.this.checkIfDiskStateChanged();
|
||||
}, () -> false, () -> AccessType.INSERT_EXTRACT);
|
||||
}
|
||||
|
||||
cache.invalidate();
|
||||
|
||||
checkIfDiskStateChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,6 +204,8 @@ public class TilePortableGrid extends TileBase implements IGrid, IPortableGrid,
|
||||
private IStorageDisk<ItemStack> storage;
|
||||
private StorageCacheItemPortable cache = new StorageCacheItemPortable(this);
|
||||
private ItemGridHandlerPortable handler = new ItemGridHandlerPortable(this, this);
|
||||
private PortableGridDiskState diskState = PortableGridDiskState.NONE;
|
||||
private boolean connected;
|
||||
|
||||
public TilePortableGrid() {
|
||||
dataManager.addWatchedParameter(ENERGY_STORED);
|
||||
@@ -207,6 +217,14 @@ public class TilePortableGrid extends TileBase implements IGrid, IPortableGrid,
|
||||
dataManager.addWatchedParameter(REDSTONE_MODE);
|
||||
}
|
||||
|
||||
public PortableGridDiskState getDiskState() {
|
||||
return diskState;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
public PortableGridType getPortableType() {
|
||||
if (type == null && world.getBlockState(pos).getBlock() == RSBlocks.PORTABLE_GRID) {
|
||||
this.type = (PortableGridType) world.getBlockState(pos).getValue(BlockPortableGrid.TYPE);
|
||||
@@ -234,6 +252,8 @@ public class TilePortableGrid extends TileBase implements IGrid, IPortableGrid,
|
||||
this.redstoneMode = RedstoneMode.read(stack.getTagCompound());
|
||||
}
|
||||
|
||||
this.diskState = getDiskState(this);
|
||||
|
||||
markDirty();
|
||||
}
|
||||
|
||||
@@ -441,7 +461,8 @@ public class TilePortableGrid extends TileBase implements IGrid, IPortableGrid,
|
||||
}
|
||||
|
||||
@Override
|
||||
public IStorage<ItemStack> getStorage() {
|
||||
@Nullable
|
||||
public IStorageDisk<ItemStack> getStorage() {
|
||||
return storage;
|
||||
}
|
||||
|
||||
@@ -454,6 +475,39 @@ public class TilePortableGrid extends TileBase implements IGrid, IPortableGrid,
|
||||
public void drainEnergy(int energy) {
|
||||
if (RS.INSTANCE.config.portableGridUsesEnergy && getPortableType() != PortableGridType.CREATIVE && redstoneMode.isEnabled(world, pos)) {
|
||||
energyStorage.extractEnergyInternal(energy);
|
||||
|
||||
checkIfDiskStateChanged();
|
||||
}
|
||||
|
||||
checkIfConnectivityChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEnergy() {
|
||||
if (RS.INSTANCE.config.portableGridUsesEnergy && getPortableType() != PortableGridType.CREATIVE) {
|
||||
return energyStorage.getEnergyStored();
|
||||
}
|
||||
|
||||
return energyStorage.getMaxEnergyStored();
|
||||
}
|
||||
|
||||
private void checkIfDiskStateChanged() {
|
||||
PortableGridDiskState newDiskState = getDiskState(this);
|
||||
|
||||
if (this.diskState != newDiskState) {
|
||||
this.diskState = newDiskState;
|
||||
|
||||
RSUtils.updateBlock(world, pos);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkIfConnectivityChanged() {
|
||||
boolean isConnected = getEnergy() != 0;
|
||||
|
||||
if (this.connected != isConnected) {
|
||||
this.connected = isConnected;
|
||||
|
||||
RSUtils.updateBlock(world, pos);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -518,6 +572,31 @@ public class TilePortableGrid extends TileBase implements IGrid, IPortableGrid,
|
||||
}
|
||||
|
||||
redstoneMode = RedstoneMode.read(tag);
|
||||
|
||||
diskState = getDiskState(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
super.onLoad();
|
||||
|
||||
checkIfConnectivityChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound writeUpdate(NBTTagCompound tag) {
|
||||
tag.setInteger(NBT_DISK_STATE, diskState.getId());
|
||||
tag.setBoolean(NBT_CONNECTED, getEnergy() != 0);
|
||||
|
||||
return super.writeUpdate(tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readUpdate(NBTTagCompound tag) {
|
||||
super.readUpdate(tag);
|
||||
|
||||
diskState = PortableGridDiskState.getById(tag.getInteger(NBT_DISK_STATE));
|
||||
connected = tag.getBoolean(NBT_CONNECTED);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -552,4 +631,25 @@ public class TilePortableGrid extends TileBase implements IGrid, IPortableGrid,
|
||||
|
||||
markDirty();
|
||||
}
|
||||
|
||||
public static PortableGridDiskState getDiskState(IPortableGrid portableGrid) {
|
||||
if (portableGrid.getStorage() == null) {
|
||||
return PortableGridDiskState.NONE;
|
||||
}
|
||||
|
||||
if (portableGrid.getEnergy() == 0) {
|
||||
return PortableGridDiskState.DISCONNECTED;
|
||||
}
|
||||
|
||||
int stored = portableGrid.getStorage().getStored();
|
||||
int capacity = portableGrid.getStorage().getCapacity();
|
||||
|
||||
if (stored == capacity) {
|
||||
return PortableGridDiskState.FULL;
|
||||
} else if ((int) ((float) stored / (float) capacity * 100F) >= 85) {
|
||||
return PortableGridDiskState.NEAR_CAPACITY;
|
||||
} else {
|
||||
return PortableGridDiskState.NORMAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user