Skip to content

Commit

Permalink
Merge pull request #204 from shogun-kub/IIA-dev
Browse files Browse the repository at this point in the history
Repellator event handler reworked.
  • Loading branch information
CppEnjoyer authored Oct 8, 2021
2 parents fd5ab07 + 37a874d commit 28a3515
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
23 changes: 19 additions & 4 deletions src/main/java/gregtech/api/util/GT_SpawnEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import gregtech.api.metatileentity.BaseMetaTileEntity;
import gregtech.common.tileentities.machines.basic.GT_MetaTileEntity_MonsterRepellent;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.entity.monster.EntityPigZombie;
import net.minecraft.entity.passive.EntityOcelot;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingSpawnEvent.CheckSpawn;
Expand All @@ -22,20 +25,32 @@ public GT_SpawnEventHandler() {

@SubscribeEvent
public void denyMobSpawn(CheckSpawn event) {
if (event.getResult() == Event.Result.ALLOW) {
if (event.getResult() == Event.Result.ALLOW || (event.entityLiving instanceof EntityPlayer)) {
return;
}
if (event.entityLiving.isCreatureType(EnumCreatureType.monster, false)) {
if (event.entityLiving.isCreatureType(EnumCreatureType.monster, false)
|| event.entityLiving instanceof EntityPigZombie || event.entityLiving instanceof EntityOcelot
)
{
boolean spawnIsNeutral = (event.entityLiving instanceof EntityPigZombie || event.entityLiving instanceof EntityOcelot);
for (int[] rep : mobReps) {
if (rep[3] == event.entity.worldObj.provider.dimensionId) {
TileEntity tTile = event.entity.worldObj.getTileEntity(rep[0], rep[1], rep[2]);
if (tTile instanceof BaseMetaTileEntity && ((BaseMetaTileEntity) tTile).getMetaTileEntity() instanceof GT_MetaTileEntity_MonsterRepellent) {
int r = ((GT_MetaTileEntity_MonsterRepellent) ((BaseMetaTileEntity) tTile).getMetaTileEntity()).mRange;
boolean neutralsAllowed =
((GT_MetaTileEntity_MonsterRepellent) ((BaseMetaTileEntity) tTile).getMetaTileEntity()).mNeutralsAllowed;
double dx = rep[0] + 0.5F - event.entity.posX;
double dy = rep[1] + 0.5F - event.entity.posY;
double dz = rep[2] + 0.5F - event.entity.posZ;
if ((dx * dx + dz * dz + dy * dy) <= Math.pow(r, 2)) {
event.setResult(Event.Result.DENY);
//Original - sphere: (dx * dx + dz * dz + dy * dy) <= Math.pow(r, 2)
//New - cube: (Math.abs(dx) <= r && Math.abs(dz) <= r && Math.abs(dy) <= r)
if (Math.abs(dx) <= r && Math.abs(dz) <= r && Math.abs(dy) <= r) {
if ((!neutralsAllowed && spawnIsNeutral) || (!spawnIsNeutral))
{
event.setResult(Event.Result.DENY);
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_TieredMachineBlock;
import gregtech.api.objects.GT_RenderedTexture;
import gregtech.api.util.GT_SpawnEventHandler;
import gregtech.api.util.GT_Utility;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.entity.player.EntityPlayer;

import java.util.Arrays;

import static gregtech.api.enums.GT_Values.V;

public class GT_MetaTileEntity_MonsterRepellent extends GT_MetaTileEntity_TieredMachineBlock {

public int mRange = 16;

public boolean mNeutralsAllowed = true;

public GT_MetaTileEntity_MonsterRepellent(int aID, String aName, String aNameRegional, int aTier) {
super(aID, aName, aNameRegional, aTier, 0, "Repels nasty Creatures. Range: " + (4 + (12 * aTier)) + " unpowered / " + (16 + (48 * aTier)) + " powered");
}
Expand All @@ -42,7 +47,8 @@ public ITexture[] getTexture(IGregTechTileEntity aBaseMetaTileEntity, byte aSide
public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTimer) {
if (aBaseMetaTileEntity.isAllowedToWork() && aBaseMetaTileEntity.isServerSide()) {
int[] tCoords = new int[]{aBaseMetaTileEntity.getXCoord(), aBaseMetaTileEntity.getYCoord(), aBaseMetaTileEntity.getZCoord(), aBaseMetaTileEntity.getWorld().provider.dimensionId};
if ((aTimer % 600 == 0) && !GT_SpawnEventHandler.mobReps.contains(tCoords)) {
if ((aTimer % 600 == 0) && !GT_SpawnEventHandler.mobReps.stream().anyMatch(c -> Arrays.equals(c, tCoords)))
{
GT_SpawnEventHandler.mobReps.add(tCoords);
}
if (aBaseMetaTileEntity.isUniversalEnergyStored(getMinimumStoredEU()) && aBaseMetaTileEntity.decreaseStoredEnergyUnits(1 << (this.mTier * 2), false)) {
Expand All @@ -56,15 +62,24 @@ public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTimer) {
@Override
public void onFirstTick(IGregTechTileEntity aBaseMetaTileEntity) {
int[] tCoords = new int[]{aBaseMetaTileEntity.getXCoord(), aBaseMetaTileEntity.getYCoord(), aBaseMetaTileEntity.getZCoord(), aBaseMetaTileEntity.getWorld().provider.dimensionId};
GT_SpawnEventHandler.mobReps.add(tCoords);
if (GT_SpawnEventHandler.mobReps != null && !GT_SpawnEventHandler.mobReps.stream().anyMatch(c -> Arrays.equals(c, tCoords))){
GT_SpawnEventHandler.mobReps.add(tCoords);
}
}

@Override
public void onRemoval() {
int[] tCoords = new int[]{this.getBaseMetaTileEntity().getXCoord(), this.getBaseMetaTileEntity().getYCoord(), this.getBaseMetaTileEntity().getZCoord(), this.getBaseMetaTileEntity().getWorld().provider.dimensionId};
GT_SpawnEventHandler.mobReps.remove(tCoords);
GT_SpawnEventHandler.mobReps.removeIf(seh -> Arrays.equals(seh,tCoords));
}

@Override
public void onScrewdriverRightClick(byte aSide, EntityPlayer aPlayer, float aX, float aY, float aZ) {
mNeutralsAllowed = !mNeutralsAllowed;
GT_Utility.sendChatToPlayer(aPlayer, mNeutralsAllowed ? trans("217","Prevents spawn of hostile creatures") : trans("218","Prevents spawn of hostile creatures, pig zombies and ocelots"));
}


@Override
public boolean isAccessAllowed(EntityPlayer aPlayer) {
return true;
Expand Down Expand Up @@ -132,9 +147,11 @@ public ITexture[][][] getTextureSet(ITexture[] aTextures) {

@Override
public void saveNBTData(NBTTagCompound aNBT) {
aNBT.setBoolean("neutralsAllowed", mNeutralsAllowed);
}

@Override
public void loadNBTData(NBTTagCompound aNBT) {
mNeutralsAllowed = aNBT.getBoolean("neutralsAllowed");
}
}

0 comments on commit 28a3515

Please sign in to comment.