Jump to content

Recommended Posts

Posted (edited)

 

Hello!I'm Japanese Modder but there is little modding information in Japan,so I ask you.

I make a chest,that can have many item.

But when I open the inventory in Multiplay,The displayed amount in GUI and the amount in the chest are different.

For example,The chest has 101,985,303 items,but GUI shows 23 items

 

TileEntity

 

public abstract class TileEntityInfinityChestBase extends TileEntity implements ISidedInventory{
	/**チェストの中に入っているアイテム*/
	private ItemStack chestItem;
	/**INスロットにあるアイテム*/
	private ItemStack inputSlot;
	/**このチェストのContainer*/
	private static ContainerInfinity container=null;

	public int maxCapacity;

	public TileEntityInfinityChestBase(){
	}

	/**Containerを設定*/
	public void setContainer(ContainerInfinity cont){
		container=cont;
	}

	@Override
	public void markDirty(){
		if(isMax()){
			//満杯なら、INスロットにアイテムを表示させる
			this.inputSlot=getCopy(this.chestItem.getMaxStackSize());
		}
		//else if((this.chestItem!=null)&&this.maxCapacity-this.chestItem.stackSize<64) {
			//this.inputSlot=getCopy(this.chestItem.stackSize-this.maxCapacity+64);
	//	}
		else{
			//満杯でなければ何も表示しない
			this.inputSlot=null;
		}
		if(container!=null){
			//コンテナのスロットにアイテムを設定する
			container.changeSlot();
		}
		List<EntityPlayer> list=this.worldObj.playerEntities;
		for(EntityPlayer player:list){
			if((player instanceof EntityPlayerMP)){
				((EntityPlayerMP)player).playerNetServerHandler.sendPacket(getDescriptionPacket());
			}
		}
	}

	@Override
	public void updateEntity(){
	}

	/**チェストのアイテムを一個コピーして返す*/
	public ItemStack getCopy(){
		return getCopy(1);
	}

	/**チェストアイテムを引数の数だけコピーして返す*/
	public ItemStack getCopy(int size){
		if(!hasStack()){
			return null;
		}
		ItemStack stack=this.chestItem.copy();
		stack.stackSize=size;
		return stack;
	}

	/**チェスト内のアイテムを取得する*/
	public ItemStack getStack(){
		return this.chestItem;
	}

	/**チェスト内のアイテムを表示欄に設定する*/
	public void setStack(ItemStack stack){
		setStack(stack,false);
	}

	/**チェスト内のアイテムを表示欄に設定する*/
	public void setStack(ItemStack stack,boolean isBlockSet){
		if(isBlockSet){
			this.chestItem=stack;
			this.markDirty();
		}else{
			this.setInventorySlotContents(0,stack);
		}
	}

	/**チェストにアイテムが入っているか*/
	public boolean hasStack(){
		if((this.chestItem!=null)&&(this.chestItem.stackSize<=0)){
			this.chestItem=null;
		}
		return this.chestItem!=null;
	}

	/**チェストが満杯かどうか*/
	public boolean isMax(){
		return (this.chestItem!=null)&&(this.chestItem.stackSize>=this.maxCapacity);
	}

	/**渡されたItemStackとチェスト内のアイテムが同じかどうかを返す*/
	public boolean isItemEqual(ItemStack stack){
		return (this.chestItem!=null)&&(isStackable(stack))
				&&(stack.isItemEqual(this.chestItem));
	}

	/**渡されたItemStackがスタック可能かを返す*/
	public boolean isStackable(ItemStack stack){
		return (stack!=null)&&(!stack.hasTagCompound())&&((!stack.isItemDamaged())||(stack.getItemDamage()==0));
	}

	public ItemStack decSize(int size){
		return decrStackSize(0,size);
	}

	/**チェスト内のアイテムを引数分追加し、その後のアイテムスタックを返す*/
	public ItemStack addSize(int size){
		if(!this.hasStack()){
			return null;
		}
		ItemStack result=null;
		if(this.chestItem.stackSize+size>this.maxCapacity){
			//チェスト内のアイテムの数と足して溢れるなら
			result=this.chestItem.copy();
			result.stackSize=(this.maxCapacity-this.chestItem.stackSize);
			//余った分を計算する
			this.chestItem.stackSize=this.maxCapacity;
			//満杯にする
		}else{
			this.chestItem.stackSize+=size;
		}
		markDirty();
		return result;
	}

	/**チェスト内のアイテムに渡されたItemStack分を追加し、余った分を返す*/
	public int addStack(ItemStack itemstack){
		if(itemstack==null){
			return 0;
		}
		int size=itemstack.stackSize;
		if(!hasStack()){
			//何も入っていなければ
			this.chestItem=itemstack.copy();
			//アイテムをセット
			if(size>this.maxCapacity){
				this.chestItem.stackSize=this.maxCapacity;
			}
			size-=this.chestItem.stackSize;
			return size;
		}
		if(!isItemEqual(itemstack)){
			//渡されたアイテムスタックと内容物が違うなら元のサイズをそのまま返す
			return size;
		}
		if(this.chestItem.stackSize+size>this.maxCapacity){
			//内容物と元のサイズを足して余るようなら
			size=this.chestItem.stackSize+size-this.maxCapacity;
			//余った分を計算
			this.chestItem.stackSize=this.maxCapacity;
		}else{
			this.chestItem.stackSize+=size;
			size=0;
		}
		this.markDirty();
		return size;
	}

	public int decStack(ItemStack stack){
		return decStack(stack,0);
	}

	public int decStack(ItemStack stack,int itemLimit){
		if((!isItemEqual(stack))||(itemLimit<0)){
			return 0;
		}
		if(itemLimit==0){
			itemLimit=stack.getMaxStackSize();
		}
		if(stack.stackSize>=itemLimit){
			return 0;
		}
		int size=stack.stackSize;
		int max=itemLimit-size;
		if(max>=this.chestItem.stackSize){
			size=this.chestItem.stackSize;
			this.chestItem=null;
		}else{
			size=max;
			this.chestItem.stackSize-=max;
		}
		this.markDirty();
		return size;
	}

	@Override
	public int getSizeInventory(){
		return 2;
	}

	@Override
	public ItemStack getStackInSlot(int slot){
//		if((this.chestItem!=null)&&this.maxCapacity-this.chestItem.stackSize<64) {
	//		ItemStack item=this.getCopy(this.chestItem.stackSize-this.maxCapacity+64);
		//	if(slot==1)
			//return item;
//		}
		return slot==1 ? this.inputSlot : this.chestItem;
	}

	@Override
	/**指定したスロットのアイテムを指定した分だけ減らす*/
	public ItemStack decrStackSize(int slot,int dec){
		if(!hasStack()){
			return null;
		}
		if(slot==1){
			//搬入スロットだったら何もしない
			return null;
		}
		if(this.chestItem.stackSize<=dec){
			//減らす分がチェストの内容量より大きければ
			ItemStack result=this.chestItem.copy();
			this.chestItem=null;
			//空にする
			markDirty();
			return result;
		}
		ItemStack result=this.chestItem.splitStack(dec);
		markDirty();
		return result;
	}

	@Override
	public ItemStack getStackInSlotOnClosing(int slot){
		return slot==1 ? this.inputSlot : this.chestItem;
	}

	@Override
	public void setInventorySlotContents(int slot,ItemStack setstack){
		if(slot==1){
			//搬入スロットにセットする場合
			int size=addStack(setstack);
			//とりあえず追加、余った分を取得
			if(size>0){
				//余っていれば
				setstack.stackSize=size;
			}else{
				if(this.chestItem!=null&&this.chestItem.stackSize<this.maxCapacity-64) {
					this.inputSlot=null;
				}
			}
		}else{
			//搬入スロットでなければ
			this.chestItem=setstack;
			if(this.chestItem!=null){
				if(this.chestItem.stackSize>this.maxCapacity){
					//最大容量超過なら
					this.chestItem.stackSize=this.maxCapacity;
					//最大容量にする
				}
			}
		}
		markDirty();
	}

	/**アイテムスタックをチェストの上にドロップさせる*/
	public void popItems(ItemStack stack,float height){
		float f=this.worldObj.rand.nextFloat()*0.8F+0.1F;
		float f1=this.worldObj.rand.nextFloat()*0.8F+0.1F;
		float f2=this.worldObj.rand.nextFloat()*0.8F+0.1F;
		EntityItem entityitem=new EntityItem(this.worldObj,this.xCoord+f,this.yCoord+f1+0.5F,this.zCoord+f2,stack);

		float f3=0.05F;
		entityitem.motionX=((float)this.worldObj.rand.nextGaussian()*f3);
		entityitem.motionY=((float)this.worldObj.rand.nextGaussian()*f3+height);
		entityitem.motionZ=((float)this.worldObj.rand.nextGaussian()*f3);
		this.worldObj.spawnEntityInWorld(entityitem);
	}

	@Override
	public int[] getAccessibleSlotsFromSide(int side){
		//上面からは搬入スロットにアクセスできる
		return new int[]{0,1};
	}

	@Override
	public boolean canExtractItem(int slot,ItemStack var2,int side){
		return slot==0;
	}

	@Override
	public boolean canInsertItem(int slot,ItemStack var2,int side){
		return isItemValidForSlot(slot,var2);
	}

	@Override
	public String getInventoryName(){
		return "";
	}

	@Override
	public boolean hasCustomInventoryName(){
		return false;
	}

	@Override
	/**指定したスロットに指定したItemStackが入るかを返す*/
	public boolean isItemValidForSlot(int slot,ItemStack var2){
		return (slot==1)&&((this.chestItem==null)
				||((this.chestItem.isItemEqual(var2))&&(ItemStack.areItemStackTagsEqual(this.chestItem,var2))));
	}

	@Override
	public void readFromNBT(NBTTagCompound nbt){
		super.readFromNBT(nbt);
		if(nbt.getTag("chestItem")!=null){
			this.chestItem=readFromNBTCustom((NBTTagCompound)nbt.getTag("chestItem"));
		}else{
			this.chestItem=null;
		}
	}

	@Override
	public void writeToNBT(NBTTagCompound nbt){
		super.writeToNBT(nbt);
		setNBT(nbt);
	}

	/**渡されたNBTに内容物の情報を書き込む*/
	public NBTTagCompound setNBT(NBTTagCompound nbt){
		NBTTagCompound nbtTagCompound=new NBTTagCompound();
		if(this.chestItem!=null){
			nbtTagCompound.setShort("id",(short)Item.getIdFromItem(this.chestItem.getItem()));
			nbtTagCompound.setInteger("Count",this.chestItem.stackSize);
			nbtTagCompound.setShort("Damage",(short)this.chestItem.getItemDamage());
			if(this.chestItem.stackTagCompound!=null){
				nbtTagCompound.setTag("tag",this.chestItem.stackTagCompound);
			}
		}
		nbt.setTag("chestItem",nbtTagCompound);
		return nbt;
	}

	/**渡されたNBTに渡されたItemStackの情報を書き込む*/
	public static NBTTagCompound setToNBTCustom(NBTTagCompound nbt,ItemStack stack){
		NBTTagCompound nbtTagCompound=new NBTTagCompound();
		if(stack!=null){
			nbtTagCompound.setShort("id",(short)Item.getIdFromItem(stack.getItem()));
			nbtTagCompound.setInteger("Count",stack.stackSize);
			nbtTagCompound.setShort("Damage",(short)stack.getItemDamage());
			if(stack.stackTagCompound!=null){
				nbtTagCompound.setTag("tag",stack.stackTagCompound);
			}
		}
		nbt.setTag("chestItem",nbtTagCompound);
		return nbt;
	}

	/**渡されたNBTから情報を書き出す*/
	public static ItemStack readFromNBTCustom(NBTTagCompound nbtTagCompound){
		if(nbtTagCompound==null){
			return null;
		}
		Item item=Item.getItemById(nbtTagCompound.getShort("id"));
		if(item==null){
			return null;
		}
		int stackSize=nbtTagCompound.getInteger("Count");
		int itemDamage=nbtTagCompound.getShort("Damage");
		if(itemDamage<0){
			itemDamage=0;
		}
		ItemStack itemStack=new ItemStack(item,stackSize,itemDamage);
		if(nbtTagCompound.hasKey("tag",10)){
			NBTTagCompound stackTagCompound=nbtTagCompound.getCompoundTag("tag");
			itemStack.setTagCompound(stackTagCompound);
		}
		return itemStack;
	}

	@Override
	/**インベントリの最大容量*/
	public int getInventoryStackLimit(){
		return this.maxCapacity;
	}

	@Override
	/**プレイヤーがこのTileEntityを使えるかどうか*/
	public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer){
		return (this.worldObj.getTileEntity(this.xCoord,this.yCoord,this.zCoord)==this)
				||(par1EntityPlayer.getDistanceSq(this.xCoord+0.5D,this.yCoord+0.5D,this.zCoord+0.5D)<=64.0D);
	}

	@Override
	public void openInventory(){
		markDirty();
	}

	@Override
	public void closeInventory(){
		container=null;
		markDirty();
	}

	@Override
	public Packet getDescriptionPacket(){
		NBTTagCompound nbtTagCompound=new NBTTagCompound();
		writeToNBT(nbtTagCompound);
		return new S35PacketUpdateTileEntity(this.xCoord,this.yCoord,this.zCoord,1,nbtTagCompound);
	}

	//パケットの送受信
	@Override
	public void onDataPacket(NetworkManager net,S35PacketUpdateTileEntity pkt){
		readFromNBT(pkt.func_148857_g());
	}
}

Container

public class ContainerInfinity extends Container{

	private TileEntityInfinityChestBase chest;
	private InventoryPlayer playerInventory;
	private IInventory guiInventory=new InventoryBasic("slots",false,3);

	public ContainerInfinity(IInventory plInv, TileEntityInfinityChestBase tileInv){
		this.chest = tileInv;
		tileInv.setContainer(this);
		tileInv.openInventory();
		this.playerInventory = ((InventoryPlayer)plInv);
		addSlotToContainer(new SlotInfinity(tileInv, 0, 12, 21, false, false));
		//ディスプレイ
		addSlotToContainer(new SlotInfinity(tileInv, 1, 80, 63, true, true));
		//INスロット
		addSlotToContainer(new SlotInfinity(this.guiInventory, 1, 134, 63, true, false));
		//OUTスロット
		changeSlot();
		for (int y = 0; y < 3; y++) {
			for (int x = 0; x < 9; x++) {
				addSlotToContainer(new Slot(plInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
			}
		}
		for (int x = 0; x < 9; x++) {
			addSlotToContainer(new Slot(plInv, x, 8 + x * 18, 142));
		}
		
	}

	/**TileEntity内にアイテムがあればディスプレイスロットに設定する
	 * おそらくサーバー側でバグっている部分*/
	public void changeSlot(){
		if(!this.chest.hasStack()){
			this.guiInventory.setInventorySlotContents(1,null);
			return;
		}
		ItemStack chestStack=this.chest.getStack();
		int maxStack=chestStack.getMaxStackSize();
		ItemStack slot2=chestStack.copy();
		System.out.println(slot2.stackSize);
		if(slot2.stackSize>=maxStack){
			slot2.stackSize=maxStack;
		}
		this.guiInventory.setInventorySlotContents(1,slot2);
	}

	@Override
	public boolean canInteractWith(EntityPlayer player){
		return this.chest.isUseableByPlayer(player);
	}

	@Override
	/**スロットをクリックした際の処理
	 * slot:スロット番号、mouse:0=左、1=右、2=ミドル、isShift:0=false,1=true*/
	public ItemStack slotClick(int slot,int mouse,int isShift,EntityPlayer player){
		/*スロット番号
		 * 0:アイテム表示欄
		 * 1:搬入スロット
		 * 2:搬出スロット
		 * 3~38:プレイヤーインベントリ*/
		if((mouse>1)||(mouse<0)){
			//マウスの値が不正だった場合の処理
			return null;
		}
		if(slot==0){
			//アイテム表示欄をクリックした際、デバッグモードを使う
			return debugmode(mouse,isShift,player);
		}
		ItemStack stack=player.inventory.getItemStack();
		//今掴んでるアイテム
		if(stack==null){
			//何も掴んでない時
			if(slot==1){
				//搬入スロットクリック時は何もしない
				return null;
			}
			if(slot==2){
				//搬出スロットクリック時
				if(isShift!=1){
					//shift押していなければ掴む
					return outPickup(mouse,player.inventory);
				}
				//プレイヤーインベントリに追加
				return addInventory(player.inventory);
			}
			if((isShift!=1)||(slot<0)){
				return super.slotClick(slot,mouse,isShift,player);
			}
			return addChest(slot,player.inventory);
		}
		if(slot==1){
			//搬入スロットクリック時
			if(isShift!=1){
				//シフトを押していなければ
				return addChest(stack,mouse,player.inventory);
			}
			return addChest(stack,player.inventory);
			//一括移動
		}
		if(slot==2){
			//搬出スロットクリック時は何もしない
			return null;
		}
		if((isShift!=1)||(slot<0)){
			return super.slotClick(slot,mouse,isShift,player);
		}
		return addInventory(stack,player.inventory);
	}

	protected ItemStack outPickup(int mouse,InventoryPlayer playerInventory){
		ItemStack slotStack=this.guiInventory.getStackInSlot(1);
		if(slotStack==null){
			return null;
		}
		int size=slotStack.stackSize;
		if(mouse==1){
			size=(size&0x1)+(size>>1);
		}
		playerInventory.setItemStack(slotStack.splitStack(size));
		ItemStack result=this.chest.decSize(size);
		changeSlot();
		return result;
	}

	/**アイテムをチェストに入れる(掴んでる分だけ)*/
	protected ItemStack addChest(ItemStack stack,int mouse,InventoryPlayer playerInventory){
		if(!this.chest.isStackable(stack)){
			return null;
		}
		if(mouse==1){
			ItemStack copy=stack.copy();
			copy.stackSize=1;
			if(addChest(copy)==null){
				playerInventory.setItemStack(decStack(stack,1));
				changeSlot();
			}
			return null;
		}
		playerInventory.setItemStack(addChest(stack));
		return null;
	}

	/**アイテムをチェストに入れる(一括移動)*/
	protected ItemStack addChest(ItemStack stack,InventoryPlayer playerInventory){
		if(addChest(stack)==null){
			playerInventory.setItemStack(null);
		}
		for(int i=0;i<36;i++){
			Slot cs=(Slot)this.inventorySlots.get(i+3);
			ItemStack slotStack=cs.getStack();
			if((this.chest.isItemEqual(slotStack))&&(addChest(i+3,playerInventory)!=null)){
				return null;
			}
		}
		return null;
	}

	/**プレイヤーの与えられたスロットのアイテムをチェストに追加する*/
	protected ItemStack addChest(int slot,InventoryPlayer playerInventory){
		
		Slot cs=(Slot)this.inventorySlots.get(slot);
		ItemStack slotStack=cs.getStack();
		ItemStack result=null;
		if(slotStack!=null){
			result=addChest(slotStack);
		}
		cs.putStack(result);
		changeSlot();
		return result;
	}

	protected ItemStack addChest(ItemStack stack){
		if(!this.chest.isStackable(stack)){
			return stack;
		}
		if(!this.chest.hasStack()){
			this.chest.setStack(stack);
			changeSlot();
			return null;
		}
		if(this.chest.getStack().stackSize>=chest.maxCapacity){
			return stack;
		}
		if(!this.chest.isItemEqual(stack)){
			return stack;
		}
		ItemStack result=this.chest.addSize(stack.stackSize);
		int size=result==null ? stack.stackSize : result.stackSize;
		ItemStack result2=decStack(stack,size);
		changeSlot();
		return result2;
	}

	/**与えられたアイテムスタックを、指定数減らして返す*/
	public ItemStack decStack(ItemStack stack,int size){
		stack.stackSize-=size;
		if(stack.stackSize<=0){
			stack=null;
		}
		return stack;
	}

	/**プレイヤーにアイテムを追加する*/
	protected ItemStack addInventory(InventoryPlayer playerInventory){
		ItemStack stack=this.guiInventory.getStackInSlot(1);
		//INスロットを確認
		if(stack==null){
			return null;
		}
		int sizeA=stack.stackSize;
		//INスロットのアイテムの数を取得
		ItemStack result=addInventory(stack);
		
		int size = result==null?0:result.stackSize;
		this.chest.decSize(sizeA-size);
		changeSlot();
		return result;
	}

	/**与えられたアイテムスタックをプレイヤーに追加する*/
	protected ItemStack addInventory(ItemStack stack,InventoryPlayer playerInventory){
		if(!this.chest.isItemEqual(stack)){
			return null;
		}
		this.chest.getStack().stackSize+=stack.stackSize;
		playerInventory.setItemStack(null);
		ItemStack result=addInventory(this.chest.getStack());
		this.chest.setStack(result);
		changeSlot();
		return result;
	}

	protected ItemStack addInventory(ItemStack stack){
		if(!this.chest.isItemEqual(stack)){
			return stack;
		}
		int stacksize=stack.stackSize;
		//追加するアイテムのスタック数
		for(int i=0;i<36;i++){
			if(stacksize<=0){
				break;
			}
			Slot cs=(Slot)this.inventorySlots.get(i+3);
			ItemStack slotStack=cs.getStack();
			if(((slotStack==null)||(this.chest.isItemEqual(slotStack)))
					&&((slotStack==null)||(slotStack.stackSize!=slotStack.getMaxStackSize()))){
				int size;
				//
				if(slotStack!=null){
					size=stack.getMaxStackSize()-slotStack.stackSize;
				}else{
					size=stack.getMaxStackSize();
				}
				if(size>0){
					ItemStack putStack=stack.copy();
					putStack.stackSize=(slotStack==null ? 0 : slotStack.stackSize);
					if(stacksize<size){
						size=stacksize;
					}
					putStack.stackSize+=size;
					decStack(stack,size);
					stacksize-=size;
					cs.putStack(putStack);
					if(stacksize<=0){
						stacksize=0;
						break;
					}
				}
			}
		}
		stack.stackSize=stacksize;
		if(stacksize<=0){
			stack=null;
		}
		return stack;
	}

	public ItemStack debugmode(int mouse,int isShift,EntityPlayer player){
		if(player.capabilities.isCreativeMode){
			if(mouse==0){
				this.chest.addSize(isShift==1 ? 100000000 : 64);
			}else{
				this.chest.decSize(isShift==1 ? 100000000 : 64);
			}
			changeSlot();
		}
		return null;
	}

	@Override
	public void onContainerClosed(EntityPlayer player){
		super.onContainerClosed(player);
		this.chest.closeInventory();
	}
}

GUI

@SideOnly(Side.CLIENT)
public class GuiInfinity extends GuiContainer{
	private IInventory player;
	private TileEntityInfinityChestBase tile;
	private static final ResourceLocation GUI=new ResourceLocation("storagemod","textures/gui/infinity.png");
	public static Logger logger = LogManager.getLogger("StorageMod");

	public GuiInfinity(IInventory playerInventory, TileEntityInfinityChestBase tileInventory){
		super(new ContainerInfinity(playerInventory, tileInventory));
		itemRender = new RenderItemInfinity();
		this.player = playerInventory;
		this.tile = tileInventory;
		this.xSize = 176;
		this.ySize = 166;
	}

	@Override
	/**GUIの描画(前面)*/
	protected void drawGuiContainerForegroundLayer(int p_146979_1_,int p_146979_2_){
		this.fontRendererObj.drawString(String.format(StatCollector.translateToLocal(this.tile.getInventoryName()),new Object[]{ClientStringUtils.formatMax(tile.getInventoryStackLimit())}),8,5,4210752);
		this.fontRendererObj.drawString(StatCollector.translateToLocal(this.player.getInventoryName()),8,72,4210752);
		if(this.tile.hasStack()){
			ItemStack stack=this.tile.getStack();
			//何故か数がおかしい
			String name=stack.getDisplayName();
			this.fontRendererObj.drawString(name,35,17,4210752);
			//アイテムの名前を表示

			String count=ClientStringUtils.formatStack(stack.stackSize);
			this.fontRendererObj.drawString(count,47,29,4210752);
			//アイテム数を表示
			String lc=ClientStringUtils.formatLC(stack.stackSize);
			this.fontRendererObj.drawString(lc,164-this.fontRendererObj.getStringWidth(lc),40,4210752);
			//アイテム数のLC換算を表示
			ArrayList info=new ArrayList();
			stack.getItem().addInformation(stack,ClientStringUtils.mc.thePlayer,info,false);
			if(info.size()>0){
				for(int i=0;i<info.size();i++){
					this.fontRendererObj.drawString((String)info.get(i),7,42+i*10,4210752);
				}
			}
		}
	}

	@Override
	/**GUIの描画(後面)*/
	protected void drawGuiContainerBackgroundLayer(float par1,int par2,int par3){
		GL11.glColor4f(1.0F,1.0F,1.0F,1.0F);
		this.mc.renderEngine.bindTexture(GUI);
		int x=(this.width-this.xSize)/2;
		int y=(this.height-this.ySize)/2;
		drawTexturedModalRect(x,y,0,0,this.xSize,this.ySize);
	}

}

I think the synchronization process doesn't work on the client and server,but true cause isn't clear

 

 

Modding environment:

Minecraft version:1.7.10

Forge 10.13.4.1558

2020-09-26_11.28.16.png

2020-09-26_11.28.20.png

Edited by ogachan1503
Posted
1 hour ago, ogachan1503 said:

Thanks for replying.

Oh....I'm sorry.

To explain, we can't support old versions forever as doing so requires people who know the version and as new versions come out, people not using the old version forget how to work with it.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Version 1.19 - Forge 41.0.63 I want to create a wolf entity that I can ride, so far it seems to be working, but the problem is that when I get on the wolf, I can’t control it. I then discovered that the issue is that the server doesn’t detect that I’m riding the wolf, so I’m struggling with synchronization. However, it seems to not be working properly. As I understand it, the server receives the packet but doesn’t register it correctly. I’m a bit new to Java, and I’ll try to provide all the relevant code and prints *The comments and prints are translated by chatgpt since they were originally in Spanish* Thank you very much in advance No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. MountableWolfEntity package com.vals.valscraft.entity; import com.vals.valscraft.network.MountSyncPacket; import com.vals.valscraft.network.NetworkHandler; import net.minecraft.client.Minecraft; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.animal.Wolf; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Entity; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.network.PacketDistributor; public class MountableWolfEntity extends Wolf { private boolean hasSaddle; private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(MountableWolfEntity.class, EntityDataSerializers.BYTE); public MountableWolfEntity(EntityType<? extends Wolf> type, Level level) { super(type, level); this.hasSaddle = false; } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(DATA_ID_FLAGS, (byte)0); } public static AttributeSupplier.Builder createAttributes() { return Wolf.createAttributes() .add(Attributes.MAX_HEALTH, 20.0) .add(Attributes.MOVEMENT_SPEED, 0.3); } @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { ItemStack itemstack = player.getItemInHand(hand); if (itemstack.getItem() == Items.SADDLE && !this.hasSaddle()) { if (!player.isCreative()) { itemstack.shrink(1); } this.setSaddle(true); return InteractionResult.SUCCESS; } else if (!level.isClientSide && this.hasSaddle()) { player.startRiding(this); MountSyncPacket packet = new MountSyncPacket(true); // 'true' means the player is mounted NetworkHandler.CHANNEL.sendToServer(packet); // Ensure the server handles the packet return InteractionResult.SUCCESS; } return InteractionResult.PASS; } @Override public void travel(Vec3 travelVector) { if (this.isVehicle() && this.getControllingPassenger() instanceof Player) { System.out.println("The wolf has a passenger."); System.out.println("The passenger is a player."); Player player = (Player) this.getControllingPassenger(); // Ensure the player is the controller this.setYRot(player.getYRot()); this.yRotO = this.getYRot(); this.setXRot(player.getXRot() * 0.5F); this.setRot(this.getYRot(), this.getXRot()); this.yBodyRot = this.getYRot(); this.yHeadRot = this.yBodyRot; float forward = player.zza; float strafe = player.xxa; if (forward <= 0.0F) { forward *= 0.25F; } this.flyingSpeed = this.getSpeed() * 0.1F; this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 1.5F); this.setDeltaMovement(new Vec3(strafe, travelVector.y, forward).scale(this.getSpeed())); this.calculateEntityAnimation(this, false); } else { // The wolf does not have a passenger or the passenger is not a player System.out.println("No player is mounted, or the passenger is not a player."); super.travel(travelVector); } } public boolean hasSaddle() { return this.hasSaddle; } public void setSaddle(boolean hasSaddle) { this.hasSaddle = hasSaddle; } @Override protected void dropEquipment() { super.dropEquipment(); if (this.hasSaddle()) { this.spawnAtLocation(Items.SADDLE); this.setSaddle(false); } } @SubscribeEvent public static void onServerTick(TickEvent.ServerTickEvent event) { if (event.phase == TickEvent.Phase.START) { MinecraftServer server = net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer(); if (server != null) { for (ServerPlayer player : server.getPlayerList().getPlayers()) { if (player.isPassenger() && player.getVehicle() instanceof MountableWolfEntity) { MountableWolfEntity wolf = (MountableWolfEntity) player.getVehicle(); System.out.println("Tick: " + player.getName().getString() + " is correctly mounted on " + wolf); } } } } } private boolean lastMountedState = false; @Override public void tick() { super.tick(); if (!this.level.isClientSide) { // Only on the server boolean isMounted = this.isVehicle() && this.getControllingPassenger() instanceof Player; // Only print if the state changed if (isMounted != lastMountedState) { if (isMounted) { Player player = (Player) this.getControllingPassenger(); // Verify the passenger is a player System.out.println("Server: Player " + player.getName().getString() + " is now mounted."); } else { System.out.println("Server: The wolf no longer has a passenger."); } lastMountedState = isMounted; } } } @Override public void addPassenger(Entity passenger) { super.addPassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(true)); } } } @Override public void removePassenger(Entity passenger) { super.removePassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is no longer mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(false)); } } } @Override public boolean isControlledByLocalInstance() { Entity entity = this.getControllingPassenger(); return entity instanceof Player; } @Override public void positionRider(Entity passenger) { if (this.hasPassenger(passenger)) { double xOffset = Math.cos(Math.toRadians(this.getYRot() + 90)) * 0.4; double zOffset = Math.sin(Math.toRadians(this.getYRot() + 90)) * 0.4; passenger.setPos(this.getX() + xOffset, this.getY() + this.getPassengersRidingOffset() + passenger.getMyRidingOffset(), this.getZ() + zOffset); } } } MountSyncPacket package com.vals.valscraft.network; import com.vals.valscraft.entity.MountableWolfEntity; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class MountSyncPacket { private final boolean isMounted; public MountSyncPacket(boolean isMounted) { this.isMounted = isMounted; } public void encode(FriendlyByteBuf buffer) { buffer.writeBoolean(isMounted); } public static MountSyncPacket decode(FriendlyByteBuf buffer) { return new MountSyncPacket(buffer.readBoolean()); } public void handle(NetworkEvent.Context context) { context.enqueueWork(() -> { ServerPlayer player = context.getSender(); // Get the player from the context if (player != null) { // Verifies if the player has dismounted if (!isMounted) { Entity vehicle = player.getVehicle(); if (vehicle instanceof MountableWolfEntity wolf) { // Logic to remove the player as a passenger wolf.removePassenger(player); System.out.println("Server: Player " + player.getName().getString() + " is no longer mounted."); } } } }); context.setPacketHandled(true); // Marks the packet as handled } } networkHandler package com.vals.valscraft.network; import com.vals.valscraft.valscraft; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.network.simple.SimpleChannel; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class NetworkHandler { private static final String PROTOCOL_VERSION = "1"; public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel( new ResourceLocation(valscraft.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); public static void init() { int packetId = 0; // Register the mount synchronization packet CHANNEL.registerMessage( packetId++, MountSyncPacket.class, MountSyncPacket::encode, MountSyncPacket::decode, (msg, context) -> msg.handle(context.get()) // Get the context with context.get() ); } }  
    • Do you use features of inventory profiles next (ipnext) or is there a change without it?
    • Remove rubidium - you are already using embeddium, which is a fork of rubidium
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.