Advertisement
Guest User

ModItemEntity with Data Key

a guest
Oct 14th, 2020
47
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ModItemEntity2 extends ProjectileItemEntity {
  2.  
  3. private ItemStack currentItem = new ItemStack(getDefaultItem(), 1);
  4. private int tickCount = 0;
  5. private boolean tickCheck = true;
  6. private static final DataParameter<Integer> MODEL_KEY = EntityDataManager.createKey(ModItemEntity2.class, DataSerializers.VARINT);
  7.  
  8. public ModItemEntity2(EntityType<? extends ModItemEntity2> type, World worldIn) {
  9. super(type, worldIn);
  10. }
  11.  
  12. public ModItemEntity2(LivingEntity livingEntityIn, World worldIn, ItemStack stack) {
  13. super(ModEntities.MOD_ITEM, livingEntityIn, worldIn);
  14. this.currentItem = stack;
  15. this.setItem(stack);
  16. }
  17.  
  18. @Override
  19. protected void registerData() {
  20. super.registerData();
  21. this.dataManager.register(MODEL_KEY, 0);
  22. }
  23.  
  24. @Override
  25. public void writeAdditional(CompoundNBT compound) {
  26. super.writeAdditional(compound);
  27. compound.putInt("ModelIndex", this.getModelKey());
  28. }
  29.  
  30. @Override
  31. public void readAdditional(CompoundNBT compound) {
  32. super.readAdditional(compound);
  33. this.setModelKey(compound.getInt("ModelIndex"));
  34. }
  35.  
  36. public int getModelKey() { return this.dataManager.get(MODEL_KEY); }
  37. public void setModelKey(int p_204215_1_) { this.dataManager.set(MODEL_KEY, p_204215_1_); }
  38.  
  39. /*
  40. @Override
  41. public void notifyDataManagerChange(DataParameter<?> key) {
  42. if (MODEL_KEY.equals(key)) {
  43. System.out.println("Model key1: " + key);
  44. System.out.println("Model key2: " + this.getModelKey());
  45. ModItem.setTag(this.currentItem, ModItem.MODEL_TAG, this.getModelKey());
  46. }
  47. super.notifyDataManagerChange(key);
  48. }
  49. */
  50.  
  51. @Override
  52. protected Item getDefaultItem() { return ModItems.MOD_ITEM; }
  53.  
  54. private void updateTag() {
  55. ModItem.setTag(this.currentItem, ModItem.MODEL_TAG, this.getModelKey());
  56. }
  57.  
  58. @Override
  59. public IPacket<?> createSpawnPacket() {
  60. return NetworkHooks.getEntitySpawningPacket(this);
  61. }
  62.  
  63. @Override
  64. public void tick() {
  65. if (this.tickCheck) {
  66. if (this.tickCount == 5) {
  67. this.setVelocity(0, 0, 0);
  68. this.velocityChanged = true;
  69. this.setNoGravity(true);
  70. } else if (this.tickCount == 20) {
  71. updateTag();
  72. } else if (this.tickCount == 40) {
  73. updateTag();
  74. this.tickCheck = false;
  75. }
  76. this.tickCount++;
  77. }
  78. super.tick();
  79. }
  80.  
  81. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement