use of net.minecraft.tileentity.MobSpawnerTileEntity in project ChaosAwakens by ChaosAwakens.
the class SpawnEggItemMixin method useOn.
@Inject(method = "useOn(Lnet/minecraft/item/ItemUseContext;)Lnet/minecraft/util/ActionResultType;", at = @At("HEAD"), cancellable = true)
public void useOn(ItemUseContext itemUseContext, CallbackInfoReturnable<ActionResultType> cir) {
World world = itemUseContext.getLevel();
if (!(world instanceof ServerWorld)) {
cir.setReturnValue(ActionResultType.SUCCESS);
} else {
ItemStack itemstack = itemUseContext.getItemInHand();
BlockPos blockpos = itemUseContext.getClickedPos();
Direction direction = itemUseContext.getClickedFace();
BlockState blockstate = world.getBlockState(blockpos);
PlayerEntity player = itemUseContext.getPlayer();
if (blockstate.is(Blocks.SPAWNER)) {
if ((CAConfig.COMMON.spawnEggsSpawnersSurvival.get() == 0) || (CAConfig.COMMON.spawnEggsSpawnersSurvival.get() == 1 && player.isCreative()) || (CAConfig.COMMON.spawnEggsSpawnersSurvival.get() == 2 && player.isCreative() && itemstack.getItem().getRegistryName().getNamespace().equals("chaosawakens")) || (CAConfig.COMMON.spawnEggsSpawnersSurvival.get() == 2 && !itemstack.getItem().getRegistryName().getNamespace().equals("chaosawakens"))) {
TileEntity tileentity = world.getBlockEntity(blockpos);
if (tileentity instanceof MobSpawnerTileEntity) {
AbstractSpawner abstractspawner = ((MobSpawnerTileEntity) tileentity).getSpawner();
EntityType<?> entitytype1 = this.getType(itemstack.getTag());
abstractspawner.setEntityId(entitytype1);
tileentity.setChanged();
world.sendBlockUpdated(blockpos, blockstate, blockstate, 3);
itemstack.shrink(1);
cir.setReturnValue(ActionResultType.CONSUME);
}
}
}
BlockPos blockpos1;
if (blockstate.getCollisionShape(world, blockpos).isEmpty()) {
blockpos1 = blockpos;
} else {
blockpos1 = blockpos.relative(direction);
}
EntityType<?> entitytype = this.getType(itemstack.getTag());
if (!blockstate.is(Blocks.SPAWNER)) {
if (entitytype.spawn((ServerWorld) world, itemstack, itemUseContext.getPlayer(), blockpos1, SpawnReason.SPAWN_EGG, true, !Objects.equals(blockpos, blockpos1) && direction == Direction.UP) != null) {
itemstack.shrink(1);
}
}
cir.setReturnValue(ActionResultType.CONSUME);
}
}
use of net.minecraft.tileentity.MobSpawnerTileEntity in project ChocolateQuestRepoured by TeamChocoQuest.
the class PreparableSpawnerInfo method vanillaSpawnerReadFromNBT.
private void vanillaSpawnerReadFromNBT(World world, DungeonPlacement placement, BlockPos pos, MobSpawnerTileEntity tileEntity) {
AbstractSpawner spawnerBaseLogic = tileEntity.getSpawnerBaseLogic();
CompoundNBT compound = new CompoundNBT();
compound.setShort("Delay", (short) 20);
if (this.tileEntityData.hasKey("MinSpawnDelay", Constants.NBT.TAG_ANY_NUMERIC)) {
compound.setShort("MinSpawnDelay", this.tileEntityData.getShort("MinSpawnDelay"));
compound.setShort("MaxSpawnDelay", this.tileEntityData.getShort("MaxSpawnDelay"));
compound.setShort("SpawnCount", this.tileEntityData.getShort("SpawnCount"));
}
if (this.tileEntityData.hasKey("MaxNearbyEntities", Constants.NBT.TAG_ANY_NUMERIC)) {
compound.setShort("MaxNearbyEntities", this.tileEntityData.getShort("MaxNearbyEntities"));
compound.setShort("RequiredPlayerRange", this.tileEntityData.getShort("RequiredPlayerRange"));
}
if (this.tileEntityData.hasKey("SpawnRange", Constants.NBT.TAG_ANY_NUMERIC)) {
compound.setShort("SpawnRange", this.tileEntityData.getShort("SpawnRange"));
}
ListNBT nbttaglist = new ListNBT();
ListNBT items = this.tileEntityData.getCompoundTag("inventory").getTagList("Items", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < items.tagCount(); i++) {
CompoundNBT itemTag = items.getCompoundTagAt(i);
CompoundNBT entityTag = itemTag.getCompoundTag("tag").getCompoundTag("EntityIn");
Entity entity = this.createEntityFromTag(world, placement, pos, entityTag);
if (entity != null) {
CompoundNBT newEntityTag = new CompoundNBT();
entity.writeToNBTAtomically(newEntityTag);
newEntityTag.removeTag("UUIDLeast");
newEntityTag.removeTag("UUIDMost");
newEntityTag.removeTag("Pos");
if (nbttaglist.isEmpty()) {
compound.setTag("SpawnData", newEntityTag);
}
nbttaglist.appendTag(new WeightedSpawnerEntity(itemTag.getByte("Count"), newEntityTag).toCompoundTag());
}
}
compound.setTag("SpawnPotentials", nbttaglist);
spawnerBaseLogic.readFromNBT(compound);
}
use of net.minecraft.tileentity.MobSpawnerTileEntity in project ChocolateQuestRepoured by TeamChocoQuest.
the class PreparableSpawnerInfo method prepare.
@Override
protected GeneratablePosInfo prepare(World world, DungeonPlacement placement, BlockPos pos) {
BlockState state;
TileEntity tileEntity;
BlockPos p = pos.toImmutable();
if (this.tileEntityData.getBoolean("vanillaSpawner")) {
state = Blocks.MOB_SPAWNER.getDefaultState();
tileEntity = state.getBlock().createTileEntity(world, state);
if (tileEntity instanceof MobSpawnerTileEntity) {
this.vanillaSpawnerReadFromNBT(world, placement, p, (MobSpawnerTileEntity) tileEntity);
}
} else {
state = CQRBlocks.SPAWNER.getDefaultState();
tileEntity = state.getBlock().createTileEntity(world, state);
if (tileEntity instanceof TileEntitySpawner) {
this.cqrSpawnerReadFromNBT(world, placement, p, (TileEntitySpawner) tileEntity);
}
}
return new GeneratableBlockInfo(p, state, tileEntity);
}
use of net.minecraft.tileentity.MobSpawnerTileEntity in project ChocolateQuestRepoured by TeamChocoQuest.
the class SpawnerFactory method createSimpleMultiUseSpawner.
/**
* Places a vanilla spawner in the provided world at the provided position using the provided ResourceLocation for the
* entity that it should spawn.
*/
public static void createSimpleMultiUseSpawner(World world, BlockPos pos, ResourceLocation entityResLoc) {
world.setBlockAndUpdate(pos, Blocks.SPAWNER.defaultBlockState());
MobSpawnerTileEntity spawner = (MobSpawnerTileEntity) world.getBlockEntity(pos);
spawner.getSpawner().setEntityId(ForgeRegistries.ENTITIES.getValue(entityResLoc));
spawner.setChanged();
// Correct method?
spawner.requestModelDataUpdate();
spawner.tick();
}
use of net.minecraft.tileentity.MobSpawnerTileEntity in project ChocolateQuestRepoured by TeamChocoQuest.
the class SpawnerFactory method convertVanillaSpawnerToCQSpawner.
/**
* Converts the vanilla spawner at the provided World/BlockPos to a CQR spawner
*/
public static void convertVanillaSpawnerToCQSpawner(World world, BlockPos pos) {
TileEntity tile = world.getBlockEntity(pos);
if (tile != null && tile instanceof MobSpawnerTileEntity) {
MobSpawnerTileEntity spawnerMultiUseTile = (MobSpawnerTileEntity) tile;
// TODO: Create mixin to retrieve this
List<WeightedSpawnerEntity> spawnerEntries = ((AccessorAbstractSpawner) spawnerMultiUseTile.getSpawner()).getSpawnPotentials();
if (!spawnerEntries.isEmpty()) {
Iterator<WeightedSpawnerEntity> iterator = spawnerEntries.iterator();
// Entity[] entities = new Entity[9];
CompoundNBT[] entityCompound = new CompoundNBT[9];
int entriesRead = 0;
while (entriesRead < 9 && iterator.hasNext()) {
/*
* Entity entity = createEntityFromNBTWithoutSpawningIt(iterator.next().getNbt(), world); entities[entriesRead] =
* entity;
*/
entityCompound[entriesRead] = iterator.next().getTag();
entriesRead++;
}
// placeSpawner(entities, false, null, world, pos);
placeSpawner(entityCompound, false, null, world, pos);
}
}
}
Aggregations