-
-
Save DiabolicaTrix/771fec8aca0f080f9c75d271a5aa1ae5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xyz.diabolicatrixlab.showcasemod.tileentity; | |
import java.util.UUID; | |
import net.minecraft.entity.EntityLivingBase; | |
import net.minecraft.entity.item.EntityItem; | |
import net.minecraft.entity.player.EntityPlayer; | |
import net.minecraft.item.Item; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.network.NetworkManager; | |
import net.minecraft.network.play.server.SPacketUpdateTileEntity; | |
import net.minecraft.util.ITickable; | |
import net.minecraftforge.items.ItemHandlerHelper; | |
import xyz.diabolicatrixlab.showcasemod.ItemRegistry; | |
import xyz.diabolicatrixlab.showcasemod.NetRegistry; | |
import xyz.diabolicatrixlab.showcasemod.ShowcaseConfig; | |
import xyz.diabolicatrixlab.showcasemod.item.ItemCoin; | |
import xyz.diabolicatrixlab.showcasemod.packet.PacketSyncOwner; | |
import xyz.diabolicatrixlab.showcasemod.packet.PacketUpdateShowcaseGui; | |
import xyz.diabolicatrixlab.showcasemod.util.CoinUtils; | |
public class TileEntityShowcase extends TileEntityBase implements ITickable { | |
public static final int SIZE = 33; | |
private String name; | |
public int coinId = 0; | |
public int price = 1; | |
public int totalPrice = 0; | |
public float customAge = 0; | |
public EntityItem item; | |
public boolean adminShop = false; | |
public UUID ownerId = UUID.randomUUID(); | |
public TileEntityShowcase() { | |
super("container.showcase", SIZE); | |
} | |
public boolean canInteractWith(EntityPlayer playerIn) { | |
return this.isUsableByPlayer(playerIn); | |
} | |
public void update(int coinId, int price){ | |
this.coinId = coinId; | |
this.price = price; | |
this.markDirty(); | |
NetRegistry.network.sendToServer(new PacketUpdateShowcaseGui(this,coinId,price)); | |
} | |
@Override | |
public void readFromNBT(NBTTagCompound compound) { | |
super.readFromNBT(compound); | |
if(compound.hasKey("Values")){ | |
int[] values = compound.getIntArray("Values"); | |
this.coinId = values[0]; | |
this.price = values[1]; | |
} | |
if(!this.getStackInSlot(0).isEmpty()){ | |
this.item = new EntityItem(this.getWorld(), 0,0,0, this.getStackInSlot(0)); | |
} | |
if(compound.hasKey("OwnerID")){ | |
this.ownerId = compound.getUniqueId("OwnerID"); | |
} | |
} | |
@Override | |
public NBTTagCompound writeToNBT(NBTTagCompound compound) { | |
super.writeToNBT(compound); | |
compound.setIntArray("Values", new int[]{this.coinId, this.price}); | |
if(this.ownerId != null) | |
{ | |
compound.setUniqueId("OwnerID", this.ownerId); | |
} | |
return compound; | |
} | |
public void removeItems(Item item, int quantity){ | |
for(int i = 1; i < this.getSizeInventory(); i++) | |
{ | |
ItemStack stack = this.getStackInSlot(i); | |
if(stack != ItemStack.EMPTY && (this.getItem() == stack.getItem())){ | |
if(this.getItemStack().getMetadata() == stack.getMetadata() && this.getItemStack().getTagCompound() == stack.getTagCompound()){ | |
if(quantity < stack.getCount()){ | |
ItemStack is = stack.copy(); | |
is.setCount(is.getCount() - quantity); | |
this.setInventorySlotContents(i, is); | |
return; | |
} | |
this.setInventorySlotContents(i, ItemStack.EMPTY); | |
quantity -= stack.getCount(); | |
} | |
} | |
} | |
} | |
public void addCoins(int quantity){ | |
int gold = Math.floorDiv(quantity, ShowcaseConfig.goldValue); | |
int silver = Math.floorDiv(quantity - (gold * ShowcaseConfig.goldValue), ShowcaseConfig.silverValue); | |
int copper = quantity - (gold * ShowcaseConfig.goldValue + silver * ShowcaseConfig.silverValue); | |
if(gold > 0) ItemHandlerHelper.insertItem(this.getHandler(), new ItemStack(ItemRegistry.goldCoin, gold), false); | |
if(silver > 0) ItemHandlerHelper.insertItem(this.getHandler(), new ItemStack(ItemRegistry.silverCoin, silver), false); | |
if(copper > 0) ItemHandlerHelper.insertItem(this.getHandler(), new ItemStack(ItemRegistry.copperCoin, copper), false); | |
} | |
@Override | |
public NBTTagCompound getUpdateTag() { | |
return writeToNBT(new NBTTagCompound()); | |
} | |
@Override | |
public SPacketUpdateTileEntity getUpdatePacket() { | |
NBTTagCompound nbt = new NBTTagCompound(); | |
this.writeToNBT(nbt); | |
return new SPacketUpdateTileEntity(this.pos, 1, nbt); | |
} | |
@Override | |
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) { | |
this.readFromNBT(pkt.getNbtCompound()); | |
} | |
@Override | |
public void update() { | |
//System.out.println("Owner: " + this.ownerId + " "); | |
} | |
public void setOwner(EntityLivingBase player){ | |
UUID uid = player.getUniqueID(); | |
this.ownerId = uid; | |
NetRegistry.network.sendToAll(new PacketSyncOwner(pos, uid)); | |
} | |
public boolean isOwner(EntityPlayer player){ | |
return player.getUniqueID().equals(this.ownerId); | |
} | |
public Item getItem(){ | |
if(this.item == null) return null; | |
return this.item.getItem().getItem(); | |
} | |
public ItemStack getItemStack(){ | |
if(this.item == null) return ItemStack.EMPTY; | |
return this.item.getItem(); | |
} | |
public int getTotal(int quantity){ | |
int value = this.coinId == 0 ? ShowcaseConfig.copperValue : this.coinId == 1 ? ShowcaseConfig.silverValue : ShowcaseConfig.goldValue; | |
return this.price * quantity * value; | |
} | |
@Override | |
public boolean isItemValidForSlot(int index, ItemStack stack) { | |
return index >= 0 && index < 16 ? !(stack.getItem() instanceof ItemCoin) : (stack.getItem() instanceof ItemCoin); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment