Skip to content

Commit

Permalink
bugfixes of entity buckets #29
Browse files Browse the repository at this point in the history
- if entities of other mods does not exist, buckets are removed
- add Upgrade Aquatic class to mod support
- changed spawning of mobs out of entity buckets to the vanilla spawn operation
  • Loading branch information
cech12 committed Oct 20, 2020
1 parent 7e07752 commit ca2bbcf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public ObtainableEntityType(@Nonnull ResourceLocation entityType, List<Fluid> fl

@Nullable
public EntityType<?> getEntityType() {
return ForgeRegistries.ENTITIES.getValue(this.entityType);
if (ForgeRegistries.ENTITIES.containsKey(this.entityType)) {
return ForgeRegistries.ENTITIES.getValue(this.entityType);
}
return null;
}

public Fluid getOneFluid() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cech12/ceramicbucket/compat/ModCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class ModCompat {
new MinecraftCompat(),
new AquacultureCompat(),
new CombustiveFishingCompat(),
new MilkAllTheMobs()
new MilkAllTheMobs(),
new UpgradeAquaticCompat()
};

public static boolean canEntityBeMilked(Entity entity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public UpgradeAquaticCompat() {
super("upgrade_aquatic");
this.addJellyfish("box_jellyfish");
this.addJellyfish("cassiopea_jellyfish");
this.addFish("glow_squid");
//this.addFish("glow_squid"); //added in 1.16
this.addJellyfish("immortal_jellyfish");
this.addFish("lionfish");
this.addFish("nautilus");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.passive.fish.AbstractFishEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluid;
Expand Down Expand Up @@ -76,12 +77,12 @@ public ActionResult<ItemStack> onItemRightClick(@Nonnull World worldIn, PlayerEn
return super.onItemRightClick(worldIn, playerIn, handIn);
}

@Override
public void onLiquidPlaced(World worldIn, @Nonnull ItemStack stack, @Nonnull BlockPos blockPos) {
if (!worldIn.isRemote) {
Entity entity = getEntityFromStack(stack, worldIn);
if (entity != null) {
entity.setPositionAndRotation(blockPos.getX() + 0.5, blockPos.getY(), blockPos.getZ() + 0.5, 0, 0);
worldIn.addEntity(entity);
EntityType<?> entityType = getEntityTypeFromStack(stack);
if (entityType != null) {
Entity entity = entityType.spawn(worldIn, stack, null, blockPos, SpawnReason.BUCKET, true, false);
if (entity instanceof AbstractFishEntity) {
((AbstractFishEntity)entity).setFromBucket(true);
} else if (entity instanceof MobEntity) {
Expand Down Expand Up @@ -143,43 +144,30 @@ public ITextComponent getDisplayName(@Nonnull ItemStack stack) {
return new TranslationTextComponent("item.ceramicbucket.ceramic_entity_bucket", name);
}

public boolean containsEntity(ItemStack stack) {
return !stack.isEmpty() && stack.hasTag() && stack.getTag().contains("entity");
}

public ItemStack putEntityInStack(ItemStack stack, Entity entity) {
CompoundNBT nbt = stack.getOrCreateTag();
nbt.putString("entity", EntityType.getKey(entity.getType()).toString());
entity.writeWithoutTypeId(nbt);
nbt.putString("EntityType", EntityType.getKey(entity.getType()).toString());
CompoundNBT entityNbt = entity.writeWithoutTypeId(new CompoundNBT());
entityNbt.remove("Pos");
entityNbt.remove("Motion");
entityNbt.remove("FallDistance");
nbt.put("EntityTag", entityNbt); //is read by spawn method
stack.setTag(nbt);
entity.remove(true);
return stack;
}

private ItemStack putEntityTypeInStack(ItemStack stack, EntityType<?> type) {
CompoundNBT nbt = stack.getOrCreateTag();
nbt.putString("entity", EntityType.getKey(type).toString());
nbt.putString("EntityType", EntityType.getKey(type).toString());
stack.setTag(nbt);
return stack;
}

@Nullable
public Entity getEntityFromStack(@Nonnull ItemStack stack, World world) {
EntityType<?> type = getEntityTypeFromStack(stack);
if (type != null) {
Entity entity = type.create(world);
if (entity != null && stack.hasTag()) {
entity.read(stack.getTag());
}
return entity;
}
return null;
}

@Nullable
public EntityType<?> getEntityTypeFromStack(ItemStack stack) {
if (stack.hasTag()) {
return EntityType.byKey(stack.getTag().getString("entity")).orElse(null);
return EntityType.byKey(stack.getTag().getString("EntityType")).orElse(null);
}
return null;
}
Expand Down

0 comments on commit ca2bbcf

Please sign in to comment.