Jump to content

Making pig zombies attack a player


XeliteXirish

Recommended Posts

I want to find a way that when a player triggers a certain action it causes pigmen in a range..I've got all that handled I just was wondering how to find a way to make the pigmen attack.

 

I've looked around EntityPigZombie and found a "becomeAngryAt" method, but this is private...any other alternitives?

 

Any help is appreciated :)

~Shaun O'Neill

View my website at -> http://www.xelitexirish.com/

You can nearly always contact me on my twitter (XeliteXirish)

Link to comment
Share on other sites

You could simply use Reflection and method.setAccessible(true);

Then you can access it from any action you want.

This probably would be the clearest way.

 

About handling action - you scan for entities around like in attackEntityFrom()

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

I did something similar with my mod at the time i didnt know how to use reflection and in fact i may switch to that but this is what i came up with. It just make them angry at the player it just makes them angry so they will attack any nearby players.

EntityPlayer player = event.entityPlayer;
List list = world.getEntitiesWithinAABB(EntityPigZombie.class, AxisAlignedBB.getBoundingBox(player.posX - 32, player.posY - 32, player.posZ - 32, player.posX + 32, player.posY + 32, player.posZ + 32));

EntityZombie entityAtPlayer = new EntityPigZombie(world);
entityAtPlayer.setPosition(player.posX, player.posY, player.posZ);

boolean flag = false;

for (int i = 0; i < list.size(); ++i) {
Entity entity1 = (Entity) list.get(i);

if (entity1 instanceof EntityPigZombie) {
	EntityPigZombie entitypigzombie = (EntityPigZombie) entity1;
	NBTTagCompound compound = new NBTTagCompound();
	entitypigzombie.writeEntityToNBT(compound);
	compound.setShort("Anger", (short) 1000);
	entitypigzombie.readEntityFromNBT(compound);
}
}

I could probably have used a for each loop instead of that foe loop.

I am the author of Draconic Evolution

Link to comment
Share on other sites

You should use Reflection instead, it will have way less overhead than that NBT hack.

Agreed i was just putting that out there. While on the topic of reflection i have quick question. I know that in order for reflection to work in both  the compiled mod and your IDE you need to give it 2 strings both the obfuscated and de-obfuscated name of the variable you are changing but in every example i have seen there is a third usually a single letter. What is that for?

I am the author of Draconic Evolution

Link to comment
Share on other sites

Thanks I actually have similer code to brandon3055, I had taught about using the NBT compound method but decided not too.  I have never actually used reflection before so I'm going to look into it, thanks though :)

 

(If you know any good documentation/tutorials on reflection it'd be nice) :)

~Shaun

View my website at -> http://www.xelitexirish.com/

You can nearly always contact me on my twitter (XeliteXirish)

Link to comment
Share on other sites

I dont know of any but its so simple you really shouldnt need one. Here is an example from my mod that sets a private value in an instance of EntityLivingBase

ReflectionHelper.setPrivateValue(EntityLivingBase.class, mob, 60, new String[]{"recentlyHit", "field_70718_bc"});

The parameters are:

The class that contains the field (must be the actual class not an extension) (is extension the rite word?)

mob is the entity

60 is the value i am setting

The two strings are the de obfuscated and obfuscated names of the field. You can find a list of all MCP name conversions in

C:\Users\[username]\.gradle\caches\minecraft\de\oceanlabs\mcp\mcp_snapshot_nodoc\20140930\fields.txt

I am the author of Draconic Evolution

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I made a block entity in forge 1.20.1, I want to prevent hopper from taking input slot item, i tried to override the extractItem method, it prevented hopper from taking input slot item, but the player also unable to take/change the item in input slot unless the slot is empty. public class FluidSeparatorBlockEntity extends BlockEntity implements MenuProvider { private static final int INPUT_SLOT = 0; private final CustomItemHandler itemHandler = new CustomItemHandler(3){ @Override protected void onContentsChanged(int slot) { setChanged(); } @Override public boolean isItemValid(int slot, @NotNull ItemStack stack) { return slot == INPUT_SLOT; } @Override public @NotNull ItemStack extractItem(int slot, int amount, boolean simulate) { if (slot == INPUT_SLOT) { return ItemStack.EMPTY; } return super.extractItem(slot, amount, simulate); } }; private LazyOptional<IItemHandler> lazyItemHandler = LazyOptional.empty(); protected final ContainerData data; private int progress = 0; private int maxProgress = 78; public FluidSeparatorBlockEntity(BlockPos pPos, BlockState pBlockState) { super(ModBlockEntities.FLUID_SEPARATOR_BE.get(), pPos, pBlockState); this.data = new ContainerData() { @Override public int get(int pIndex) { return switch (pIndex) { case 0 -> FluidSeparatorBlockEntity.this.progress; case 1, 2 -> FluidSeparatorBlockEntity.this.maxProgress; default -> 0; }; } @Override public void set(int pIndex, int pValue) { switch (pIndex) { case 0 -> FluidSeparatorBlockEntity.this.progress = pValue; case 1, 2 -> FluidSeparatorBlockEntity.this.maxProgress = pValue; } } @Override public int getCount() { return 3; } }; } @Override public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) { if(cap == ForgeCapabilities.ITEM_HANDLER) { return lazyItemHandler.cast(); } return super.getCapability(cap, side); } @Override public void onLoad() { super.onLoad(); lazyItemHandler = LazyOptional.of(() -> itemHandler); } @Override public void invalidateCaps() { super.invalidateCaps(); lazyItemHandler.invalidate(); } public void drops() { SimpleContainer inventory = new SimpleContainer(itemHandler.getSlots()); for(int i = 0; i < itemHandler.getSlots(); i++) { inventory.setItem(i, itemHandler.getStackInSlot(i)); } Containers.dropContents(this.level, this.worldPosition, inventory); } @Override public Component getDisplayName() { return Component.translatable("block.chemmaster.fluid_separator"); } @Nullable @Override public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInventory, Player pPlayer) { return new FluidSeparatorMenu(pContainerId, pPlayerInventory, this, this.data); } @Override protected void saveAdditional(CompoundTag pTag) { pTag.put("inventory", itemHandler.serializeNBT()); pTag.putInt("fluid_separator.progress", progress); super.saveAdditional(pTag); } @Override public void load(CompoundTag pTag) { super.load(pTag); itemHandler.deserializeNBT(pTag.getCompound("inventory")); progress = pTag.getInt("fluid_separator.progress"); } public void tick(Level pLevel, BlockPos pPos, BlockState pState) { ItemStack inputStack = this.itemHandler.getStackInSlot(INPUT_SLOT); if (inputStack.getCount() < 2) { resetProgress(); return; } if(hasRecipe()) { increaseCraftingProgress(); setChanged(pLevel, pPos, pState); if(hasProgressFinished()) { craftItem(); resetProgress(); } } else { resetProgress(); } } private void resetProgress() { progress = 0; } private void craftItem() { Optional<FluidSeparatingRecipe> recipe = getCurrentRecipe(); if (recipe.isPresent()) { List<ItemStack> results = recipe.get().getOutputs(); ItemStack inputStack = this.itemHandler.getStackInSlot(INPUT_SLOT); if (inputStack.getCount() < 2) { // If there are not enough items, do not proceed with crafting return; } // Extract the input item from the input slot this.itemHandler.internalExtractItem(INPUT_SLOT, 2, false); // Loop through each result item and find suitable output slots for (ItemStack result : results) { int outputSlot = findSuitableOutputSlot(result); if (outputSlot != -1) { this.itemHandler.setStackInSlot(outputSlot, new ItemStack(result.getItem(), this.itemHandler.getStackInSlot(outputSlot).getCount() + result.getCount())); } else { // Handle the case where no suitable output slot is found // This can be logging an error, throwing an exception, or any other handling logic System.err.println("No suitable output slot found for item: " + result); } } } } private int findSuitableOutputSlot(ItemStack result) { // Implement logic to find a suitable output slot for the given result // Return the slot index or -1 if no suitable slot is found for (int i = 0; i < this.itemHandler.getSlots(); i++) { // Ensure we do not place the output item in the input slot if (i == INPUT_SLOT) { continue; } ItemStack stackInSlot = this.itemHandler.getStackInSlot(i); if (stackInSlot.isEmpty() || (stackInSlot.getItem() == result.getItem() && stackInSlot.getCount() + result.getCount() <= stackInSlot.getMaxStackSize())) { return i; } } return -1; } private boolean hasRecipe() { Optional<FluidSeparatingRecipe> recipe = getCurrentRecipe(); if (recipe.isEmpty()) { return false; } List<ItemStack> results = recipe.get().getOutputs(); for (ItemStack result : results) { if (!canInsertAmountIntoOutputSlot(result) || !canInsertItemIntoOutputSlot(result.getItem())) { return false; } } return true; } private Optional<FluidSeparatingRecipe> getCurrentRecipe(){ SimpleContainer inventory = new SimpleContainer(this.itemHandler.getSlots()); for (int i = 0; i < itemHandler.getSlots(); i++) { inventory.setItem(i, this.itemHandler.getStackInSlot(i)); } return this.level.getRecipeManager().getRecipeFor(FluidSeparatingRecipe.Type.INSTANCE, inventory, level); } private boolean canInsertAmountIntoOutputSlot(ItemStack result) { for (int i = 1; i < this.itemHandler.getSlots(); i++) { ItemStack stackInSlot = this.itemHandler.getStackInSlot(i); if (stackInSlot.isEmpty() || (stackInSlot.getItem() == result.getItem() && stackInSlot.getCount() + result.getCount() <= stackInSlot.getMaxStackSize())) { return true; } } return false; } private boolean canInsertItemIntoOutputSlot(Item item) { for (int i = 1; i < this.itemHandler.getSlots(); i++) { ItemStack stackInSlot = this.itemHandler.getStackInSlot(i); if (stackInSlot.isEmpty() || stackInSlot.getItem() == item) { return true; } } return false; } private boolean hasProgressFinished() { return progress >= maxProgress; } private void increaseCraftingProgress() { progress++; } }  
    • No dice. Unfortunately this fix didn't work, thank you though.
    • Maybe you need a rayon mod. https://www.curseforge.com/minecraft/mc-mods/rayon
    • Not sure what's going on the logs are making even less sense than usual to me. Any help would be much appreciated.   https://paste.ee/p/KBHyP#s=0
  • Topics

×
×
  • Create New...

Important Information

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