mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-31 21:52:59 +00:00 
			
		
		
		
	Tick pocket computers in item entity form
See #995. And no, just because I'm adding this doesn't mean it's not a terrible issue.
This commit is contained in:
		| @@ -17,7 +17,6 @@ import dan200.computercraft.shared.common.IColouredItem; | |||||||
| import dan200.computercraft.shared.computer.core.ClientComputer; | import dan200.computercraft.shared.computer.core.ClientComputer; | ||||||
| import dan200.computercraft.shared.computer.core.ComputerFamily; | import dan200.computercraft.shared.computer.core.ComputerFamily; | ||||||
| import dan200.computercraft.shared.computer.core.ComputerState; | import dan200.computercraft.shared.computer.core.ComputerState; | ||||||
| import dan200.computercraft.shared.computer.core.ServerComputer; |  | ||||||
| import dan200.computercraft.shared.computer.items.IComputerItem; | import dan200.computercraft.shared.computer.items.IComputerItem; | ||||||
| import dan200.computercraft.shared.network.container.ComputerContainerData; | import dan200.computercraft.shared.network.container.ComputerContainerData; | ||||||
| import dan200.computercraft.shared.pocket.apis.PocketAPI; | import dan200.computercraft.shared.pocket.apis.PocketAPI; | ||||||
| @@ -25,6 +24,7 @@ import dan200.computercraft.shared.pocket.core.PocketServerComputer; | |||||||
| import dan200.computercraft.shared.pocket.inventory.PocketComputerMenuProvider; | import dan200.computercraft.shared.pocket.inventory.PocketComputerMenuProvider; | ||||||
| import net.minecraft.client.util.ITooltipFlag; | import net.minecraft.client.util.ITooltipFlag; | ||||||
| import net.minecraft.entity.Entity; | import net.minecraft.entity.Entity; | ||||||
|  | import net.minecraft.entity.item.ItemEntity; | ||||||
| import net.minecraft.entity.player.PlayerEntity; | import net.minecraft.entity.player.PlayerEntity; | ||||||
| import net.minecraft.inventory.IInventory; | import net.minecraft.inventory.IInventory; | ||||||
| import net.minecraft.item.Item; | import net.minecraft.item.Item; | ||||||
| @@ -83,53 +83,65 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     private boolean tick( @Nonnull ItemStack stack, @Nonnull World world, @Nonnull Entity entity, @Nonnull PocketServerComputer computer ) | ||||||
|  |     { | ||||||
|  |         IPocketUpgrade upgrade = getUpgrade( stack ); | ||||||
|  | 
 | ||||||
|  |         computer.setWorld( world ); | ||||||
|  |         computer.updateValues( entity, stack, upgrade ); | ||||||
|  | 
 | ||||||
|  |         boolean changed = false; | ||||||
|  | 
 | ||||||
|  |         // Sync ID | ||||||
|  |         int id = computer.getID(); | ||||||
|  |         if( id != getComputerID( stack ) ) | ||||||
|  |         { | ||||||
|  |             changed = true; | ||||||
|  |             setComputerID( stack, id ); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         // Sync label | ||||||
|  |         String label = computer.getLabel(); | ||||||
|  |         if( !Objects.equal( label, getLabel( stack ) ) ) | ||||||
|  |         { | ||||||
|  |             changed = true; | ||||||
|  |             setLabel( stack, label ); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         // Update pocket upgrade | ||||||
|  |         if( upgrade != null ) upgrade.update( computer, computer.getPeripheral( ComputerSide.BACK ) ); | ||||||
|  | 
 | ||||||
|  |         return changed; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void inventoryTick( @Nonnull ItemStack stack, World world, @Nonnull Entity entity, int slotNum, boolean selected ) |     public void inventoryTick( @Nonnull ItemStack stack, World world, @Nonnull Entity entity, int slotNum, boolean selected ) | ||||||
|     { |     { | ||||||
|         if( !world.isClientSide ) |         if( !world.isClientSide ) | ||||||
|         { |         { | ||||||
|             // Server side |  | ||||||
|             IInventory inventory = entity instanceof PlayerEntity ? ((PlayerEntity) entity).inventory : null; |             IInventory inventory = entity instanceof PlayerEntity ? ((PlayerEntity) entity).inventory : null; | ||||||
|             PocketServerComputer computer = createServerComputer( world, inventory, entity, stack ); |             PocketServerComputer computer = createServerComputer( world, entity, inventory, stack ); | ||||||
|             if( computer != null ) |             computer.keepAlive(); | ||||||
|             { |  | ||||||
|                 IPocketUpgrade upgrade = getUpgrade( stack ); |  | ||||||
| 
 | 
 | ||||||
|                 // Ping computer |             boolean changed = tick( stack, world, entity, computer ); | ||||||
|                 computer.keepAlive(); |             if( changed && inventory != null ) inventory.setChanged(); | ||||||
|                 computer.setWorld( world ); |  | ||||||
|                 computer.updateValues( entity, stack, upgrade ); |  | ||||||
| 
 |  | ||||||
|                 // Sync ID |  | ||||||
|                 int id = computer.getID(); |  | ||||||
|                 if( id != getComputerID( stack ) ) |  | ||||||
|                 { |  | ||||||
|                     setComputerID( stack, id ); |  | ||||||
|                     if( inventory != null ) inventory.setChanged(); |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 // Sync label |  | ||||||
|                 String label = computer.getLabel(); |  | ||||||
|                 if( !Objects.equal( label, getLabel( stack ) ) ) |  | ||||||
|                 { |  | ||||||
|                     setLabel( stack, label ); |  | ||||||
|                     if( inventory != null ) inventory.setChanged(); |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 // Update pocket upgrade |  | ||||||
|                 if( upgrade != null ) |  | ||||||
|                 { |  | ||||||
|                     upgrade.update( computer, computer.getPeripheral( ComputerSide.BACK ) ); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
|         else |         else | ||||||
|         { |         { | ||||||
|             // Client side |  | ||||||
|             createClientComputer( stack ); |             createClientComputer( stack ); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     @Override | ||||||
|  |     public boolean onEntityItemUpdate( ItemStack stack, ItemEntity entity ) | ||||||
|  |     { | ||||||
|  |         if( entity.level.isClientSide ) return false; | ||||||
|  | 
 | ||||||
|  |         PocketServerComputer computer = getServerComputer( stack ); | ||||||
|  |         if( computer != null && tick( stack, entity.level, entity, computer ) ) entity.setItem( stack.copy() ); | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     @Nonnull |     @Nonnull | ||||||
|     @Override |     @Override | ||||||
|     public ActionResult<ItemStack> use( World world, PlayerEntity player, @Nonnull Hand hand ) |     public ActionResult<ItemStack> use( World world, PlayerEntity player, @Nonnull Hand hand ) | ||||||
| @@ -137,22 +149,18 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I | |||||||
|         ItemStack stack = player.getItemInHand( hand ); |         ItemStack stack = player.getItemInHand( hand ); | ||||||
|         if( !world.isClientSide ) |         if( !world.isClientSide ) | ||||||
|         { |         { | ||||||
|             PocketServerComputer computer = createServerComputer( world, player.inventory, player, stack ); |             PocketServerComputer computer = createServerComputer( world, player, player.inventory, stack ); | ||||||
|  |             computer.turnOn(); | ||||||
| 
 | 
 | ||||||
|             boolean stop = false; |             boolean stop = false; | ||||||
|             if( computer != null ) |             IPocketUpgrade upgrade = getUpgrade( stack ); | ||||||
|  |             if( upgrade != null ) | ||||||
|             { |             { | ||||||
|                 computer.turnOn(); |                 computer.updateValues( player, stack, upgrade ); | ||||||
| 
 |                 stop = upgrade.onRightClick( world, computer, computer.getPeripheral( ComputerSide.BACK ) ); | ||||||
|                 IPocketUpgrade upgrade = getUpgrade( stack ); |  | ||||||
|                 if( upgrade != null ) |  | ||||||
|                 { |  | ||||||
|                     computer.updateValues( player, stack, upgrade ); |  | ||||||
|                     stop = upgrade.onRightClick( world, computer, computer.getPeripheral( ComputerSide.BACK ) ); |  | ||||||
|                 } |  | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             if( !stop && computer != null ) |             if( !stop ) | ||||||
|             { |             { | ||||||
|                 boolean isTypingOnly = hand == Hand.OFF_HAND; |                 boolean isTypingOnly = hand == Hand.OFF_HAND; | ||||||
|                 new ComputerContainerData( computer ).open( player, new PocketComputerMenuProvider( computer, stack, this, hand, isTypingOnly ) ); |                 new ComputerContainerData( computer ).open( player, new PocketComputerMenuProvider( computer, stack, this, hand, isTypingOnly ) ); | ||||||
| @@ -210,17 +218,17 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I | |||||||
|         return super.getCreatorModId( stack ); |         return super.getCreatorModId( stack ); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public PocketServerComputer createServerComputer( final World world, IInventory inventory, Entity entity, @Nonnull ItemStack stack ) |     @Nonnull | ||||||
|  |     public PocketServerComputer createServerComputer( World world, Entity entity, @Nullable IInventory inventory, @Nonnull ItemStack stack ) | ||||||
|     { |     { | ||||||
|         if( world.isClientSide ) return null; |         if( world.isClientSide ) throw new IllegalStateException( "Cannot call createServerComputer on the client" ); | ||||||
| 
 | 
 | ||||||
|         PocketServerComputer computer; |         PocketServerComputer computer; | ||||||
|         int instanceID = getInstanceID( stack ); |         int instanceID = getInstanceID( stack ); | ||||||
|         int sessionID = getSessionID( stack ); |         int sessionID = getSessionID( stack ); | ||||||
|         int correctSessionID = ComputerCraft.serverComputerRegistry.getSessionID(); |         int correctSessionID = ComputerCraft.serverComputerRegistry.getSessionID(); | ||||||
| 
 | 
 | ||||||
|         if( instanceID >= 0 && sessionID == correctSessionID && |         if( instanceID >= 0 && sessionID == correctSessionID && ComputerCraft.serverComputerRegistry.contains( instanceID ) ) | ||||||
|             ComputerCraft.serverComputerRegistry.contains( instanceID ) ) |  | ||||||
|         { |         { | ||||||
|             computer = (PocketServerComputer) ComputerCraft.serverComputerRegistry.get( instanceID ); |             computer = (PocketServerComputer) ComputerCraft.serverComputerRegistry.get( instanceID ); | ||||||
|         } |         } | ||||||
| @@ -238,13 +246,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I | |||||||
|                 computerID = ComputerCraftAPI.createUniqueNumberedSaveDir( world, "computer" ); |                 computerID = ComputerCraftAPI.createUniqueNumberedSaveDir( world, "computer" ); | ||||||
|                 setComputerID( stack, computerID ); |                 setComputerID( stack, computerID ); | ||||||
|             } |             } | ||||||
|             computer = new PocketServerComputer( |             computer = new PocketServerComputer( world, computerID, getLabel( stack ), instanceID, getFamily() ); | ||||||
|                 world, |  | ||||||
|                 computerID, |  | ||||||
|                 getLabel( stack ), |  | ||||||
|                 instanceID, |  | ||||||
|                 getFamily() |  | ||||||
|             ); |  | ||||||
|             computer.updateValues( entity, stack, getUpgrade( stack ) ); |             computer.updateValues( entity, stack, getUpgrade( stack ) ); | ||||||
|             computer.addAPI( new PocketAPI( computer ) ); |             computer.addAPI( new PocketAPI( computer ) ); | ||||||
|             ComputerCraft.serverComputerRegistry.add( instanceID, computer ); |             ComputerCraft.serverComputerRegistry.add( instanceID, computer ); | ||||||
| @@ -254,15 +256,17 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I | |||||||
|         return computer; |         return computer; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public static ServerComputer getServerComputer( @Nonnull ItemStack stack ) |     @Nullable | ||||||
|  |     public static PocketServerComputer getServerComputer( @Nonnull ItemStack stack ) | ||||||
|     { |     { | ||||||
|         int session = getSessionID( stack ); |         int session = getSessionID( stack ); | ||||||
|         if( session != ComputerCraft.serverComputerRegistry.getSessionID() ) return null; |         if( session != ComputerCraft.serverComputerRegistry.getSessionID() ) return null; | ||||||
| 
 | 
 | ||||||
|         int instanceID = getInstanceID( stack ); |         int instanceID = getInstanceID( stack ); | ||||||
|         return instanceID >= 0 ? ComputerCraft.serverComputerRegistry.get( instanceID ) : null; |         return instanceID >= 0 ? (PocketServerComputer) ComputerCraft.serverComputerRegistry.get( instanceID ) : null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     @Nullable | ||||||
|     public static ClientComputer createClientComputer( @Nonnull ItemStack stack ) |     public static ClientComputer createClientComputer( @Nonnull ItemStack stack ) | ||||||
|     { |     { | ||||||
|         int instanceID = getInstanceID( stack ); |         int instanceID = getInstanceID( stack ); | ||||||
| @@ -277,6 +281,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I | |||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     @Nullable | ||||||
|     private static ClientComputer getClientComputer( @Nonnull ItemStack stack ) |     private static ClientComputer getClientComputer( @Nonnull ItemStack stack ) | ||||||
|     { |     { | ||||||
|         int instanceID = getInstanceID( stack ); |         int instanceID = getInstanceID( stack ); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jonathan Coates
					Jonathan Coates