-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2334 from TelepathicGrunt/trunk/1.20
Optimize anti beacon to not deadlock worldgen or lag entities
- Loading branch information
Showing
5 changed files
with
163 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/com/lothrazar/cyclic/capabilities/livingentity/LivingEntityCapProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.lothrazar.cyclic.capabilities.livingentity; | ||
|
||
import net.minecraft.core.Direction; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraftforge.common.capabilities.Capability; | ||
import net.minecraftforge.common.capabilities.CapabilityManager; | ||
import net.minecraftforge.common.capabilities.CapabilityToken; | ||
import net.minecraftforge.common.capabilities.ICapabilityProvider; | ||
import net.minecraftforge.common.util.INBTSerializable; | ||
import net.minecraftforge.common.util.LazyOptional; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
public class LivingEntityCapProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> { | ||
|
||
public static Capability<LivingEntityCapabilityStorage> CYCLIC_LIVING_ENTITY = CapabilityManager.get(new CapabilityToken<>() {}); | ||
private LivingEntityCapabilityStorage livingEntityAntiBeaconPosition = null; | ||
private final LazyOptional<LivingEntityCapabilityStorage> opt = LazyOptional.of(this::createMe); | ||
|
||
@Nonnull | ||
private LivingEntityCapabilityStorage createMe() { | ||
if (livingEntityAntiBeaconPosition == null) { | ||
livingEntityAntiBeaconPosition = new LivingEntityCapabilityStorage(); | ||
} | ||
return livingEntityAntiBeaconPosition; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap) { | ||
if (cap == CYCLIC_LIVING_ENTITY) { | ||
return opt.cast(); | ||
} | ||
return LazyOptional.empty(); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { | ||
return getCapability(cap); | ||
} | ||
|
||
@Override | ||
public CompoundTag serializeNBT() { | ||
CompoundTag nbt = new CompoundTag(); | ||
createMe().saveNBTData(nbt); | ||
return nbt; | ||
} | ||
|
||
@Override | ||
public void deserializeNBT(CompoundTag nbt) { | ||
createMe().loadNBTData(nbt); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...in/java/com/lothrazar/cyclic/capabilities/livingentity/LivingEntityCapabilityStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.lothrazar.cyclic.capabilities.livingentity; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.nbt.NbtUtils; | ||
|
||
public class LivingEntityCapabilityStorage { | ||
|
||
BlockPos closestAntiBeaconPosition = null; | ||
|
||
public LivingEntityCapabilityStorage() {} | ||
|
||
public BlockPos getClosestAntiBeaconPosition() { | ||
return closestAntiBeaconPosition; | ||
} | ||
|
||
public void setClosestAntiBeaconPosition(BlockPos closestAntiBeaconPosition) { | ||
this.closestAntiBeaconPosition = closestAntiBeaconPosition; | ||
} | ||
|
||
public void saveNBTData(CompoundTag compound) { | ||
if (closestAntiBeaconPosition != null) { | ||
compound.put("closestAntiBeaconPosition", NbtUtils.writeBlockPos(closestAntiBeaconPosition)); | ||
} | ||
} | ||
|
||
public void loadNBTData(CompoundTag compound) { | ||
if (compound.contains("closestAntiBeaconPosition")) { | ||
closestAntiBeaconPosition = NbtUtils.readBlockPos(compound.getCompound("closestAntiBeaconPosition")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters