Files
thaumicadditions/src/main/java/com/pengu/thaumcraft/additions/tileentity/TileCreativeNode.java
T
layue13andClaude Sonnet 5.5 0ae97629e3
Build / build (push) Skipped
Build / publish (push) Skipped
Build / build (pull_request) Successful in 2m11s
Build / publish (pull_request) Skipped
perf: creative node verifies its registration instead of trusting it forever
#29 registered the location once. The old per-tick rewrite also repaired the shared
TileNode.locations map when another mod cleared or altered it. One lookup and four
int compares per tick restore that: generateId runs again as soon as the entry is
missing or different.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-07 23:10:59 +08:00

298 lines
9.3 KiB
Java

package com.pengu.thaumcraft.additions.tileentity;
import com.pengu.thaumcraft.additions.api.achievements.ThaumicAdditionsAchievements;
import com.pengu.thaumcraft.additions.utils.TALog;
import java.awt.Color;
import java.util.ArrayList;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.stats.StatBase;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.MovingObjectPosition.MovingObjectType;
import net.minecraft.world.World;
import thaumcraft.api.aspects.Aspect;
import thaumcraft.api.aspects.AspectList;
import thaumcraft.api.nodes.INode;
import thaumcraft.api.nodes.NodeModifier;
import thaumcraft.api.nodes.NodeType;
import thaumcraft.api.visnet.TileVisNode;
import thaumcraft.api.wands.IWandable;
import thaumcraft.common.items.wands.ItemWandCasting;
import thaumcraft.common.lib.utils.EntityUtils;
import thaumcraft.common.tiles.TileNode;
public class TileCreativeNode extends TileVisNode implements INode, IWandable {
public static int creativeNodeDrainDelay = 1;
public static int creativeNodeDrainAmount = 100;
public int rotation = 0;
private String id;
public int drainColor = 16777215;
public Entity drainEntity = null;
public MovingObjectPosition drainCollision = null;
public Color targetColor = new Color(16777215);
public Color color = new Color(16777215);
public boolean hasUpdatedOnce;
private boolean locationRegistered;
public void updateEntity() {
if(this.worldObj.isRemote && this.rotation++ >= 360) {
this.rotation = 0;
}
if(!this.hasUpdatedOnce) {
this.hasUpdatedOnce = !this.hasUpdatedOnce;
} else if(!this.locationRegistered || !this.isRegistrationIntact()) {
this.generateId();
}
}
/**
* True while the shared location map still holds this node's entry unchanged. The old code rewrote the entry every
* tick, which also repaired it after another mod cleared or altered the map; checking it is one lookup and four
* int compares, and generateId runs again as soon as the entry is missing or different.
*/
private boolean isRegistrationIntact() {
if(this.id == null || TileNode.locations == null || this.worldObj == null) {
return false;
}
java.util.List entry = (java.util.List)TileNode.locations.get(this.id);
if(entry == null || entry.size() != 4) {
return false;
}
return Integer.valueOf(this.worldObj.provider.dimensionId).equals(entry.get(0))
&& Integer.valueOf(this.xCoord).equals(entry.get(1))
&& Integer.valueOf(this.yCoord).equals(entry.get(2))
&& Integer.valueOf(this.zCoord).equals(entry.get(3));
}
public void invalidate() {
this.unregisterLocation();
super.invalidate();
}
public void onChunkUnload() {
this.unregisterLocation();
super.onChunkUnload();
}
private void unregisterLocation() {
if(this.id != null && TileNode.locations != null) {
TileNode.locations.remove(this.id);
}
this.locationRegistered = false;
}
public AspectList getAspects() {
return (new AspectList()).add(Aspect.AIR, 134217).add(Aspect.WATER, 134217).add(Aspect.FIRE, 134217).add(Aspect.ORDER, 134217).add(Aspect.ENTROPY, 134217).add(Aspect.EARTH, 134217);
}
public void setAspects(AspectList paramAspectList) {}
public boolean doesContainerAccept(Aspect paramAspect) {
return false;
}
public int addToContainer(Aspect paramAspect, int paramInt) {
return 0;
}
public boolean takeFromContainer(Aspect paramAspect, int paramInt) {
return true;
}
public boolean takeFromContainer(AspectList paramAspectList) {
return true;
}
public boolean doesContainerContainAmount(Aspect paramAspect, int paramInt) {
return true;
}
public boolean doesContainerContain(AspectList paramAspectList) {
return true;
}
public int containerContains(Aspect paramAspect) {
return Integer.MAX_VALUE;
}
public int onWandRightClick(World paramWorld, ItemStack paramItemStack, EntityPlayer paramEntityPlayer, int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5) {
return 0;
}
public ItemStack onWandRightClick(World paramWorld, ItemStack i, EntityPlayer p) {
p.setItemInUse(i, ((ItemWandCasting)i.getItem()).getMaxItemUseDuration(i));
ItemWandCasting wand = (ItemWandCasting)i.getItem();
wand.setObjectInUse(i, this.xCoord, this.yCoord, this.zCoord);
p.addStat((StatBase)ThaumicAdditionsAchievements.mapping.get("creative.node"), 1);
TALog.error(p.getCommandSenderName() + " started to tap creative aura node with id " + this.id, new Object[0]);
return i;
}
public void onUsingWandTick(ItemStack it, EntityPlayer p, int count) {
boolean mfu = false;
ItemWandCasting wand = (ItemWandCasting)it.getItem();
MovingObjectPosition movingobjectposition = EntityUtils.getMovingObjectPositionFromPlayer(this.worldObj, p, true);
int r;
int g;
int b;
if(movingobjectposition != null && movingobjectposition.typeOfHit == MovingObjectType.BLOCK) {
r = movingobjectposition.blockX;
g = movingobjectposition.blockY;
b = movingobjectposition.blockZ;
if(r != this.xCoord || g != this.yCoord || b != this.zCoord) {
p.stopUsingItem();
}
} else {
p.stopUsingItem();
}
int r2;
if(count % creativeNodeDrainDelay == 0) {
boolean r1 = false;
Aspect g1 = null;
if((g1 = this.chooseRandomFilteredFromSource(wand.getAspectsWithRoom(it), false)) != null) {
b = this.getAspects().getAmount(g1);
r2 = wand.addVis(it, g1, creativeNodeDrainAmount, !this.worldObj.isRemote);
if(r2 < creativeNodeDrainAmount) {
this.drainColor = g1.getColor();
if(!this.worldObj.isRemote) {
this.takeFromContainer(g1, creativeNodeDrainAmount - r2);
mfu = true;
}
r1 = true;
}
}
if(r1) {
this.drainEntity = p;
this.drainCollision = movingobjectposition;
this.targetColor = new Color(this.drainColor);
} else {
this.drainEntity = null;
this.drainCollision = null;
}
if(mfu) {
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
this.markDirty();
}
}
if(p.worldObj.isRemote) {
r = this.targetColor.getRed();
g = this.targetColor.getGreen();
b = this.targetColor.getBlue();
r2 = this.color.getRed() * 4;
int g2 = this.color.getGreen() * 4;
int b2 = this.color.getBlue() * 4;
this.color = new Color((r + r2) / 5, (g + g2) / 5, (b + b2) / 5);
}
}
public Aspect chooseRandomFilteredFromSource(AspectList filter, boolean preserve) {
ArrayList validaspects = new ArrayList();
Aspect[] var4 = this.getAspects().getAspects();
int var5 = var4.length;
for(int var6 = 0; var6 < var5; ++var6) {
Aspect prim = var4[var6];
if(filter.getAmount(prim) > 0) {
validaspects.add(prim);
}
}
if(validaspects.size() == 0) {
return null;
} else {
return (Aspect)validaspects.get(this.worldObj.rand.nextInt(validaspects.size()));
}
}
public void onWandStoppedUsing(ItemStack paramItemStack, World paramWorld, EntityPlayer paramEntityPlayer, int paramInt) {}
public String getId() {
return this.id;
}
public AspectList getAspectsBase() {
return this.getAspects();
}
public NodeType getNodeType() {
return NodeType.PURE;
}
public void setNodeType(NodeType paramNodeType) {}
public void setNodeModifier(NodeModifier paramNodeModifier) {}
public NodeModifier getNodeModifier() {
return NodeModifier.BRIGHT;
}
public int getNodeVisBase(Aspect paramAspect) {
return Integer.MAX_VALUE;
}
public void setNodeVisBase(Aspect paramAspect, short paramShort) {}
public void writeCustomNBT(NBTTagCompound nbt) {
if(this.id == null) {
this.generateId();
}
nbt.setString("nodeId", this.id);
}
public void readCustomNBT(NBTTagCompound nbt) {
this.id = nbt.getString("nodeId");
if(this.id == null || this.id.isEmpty()) {
this.generateId();
}
}
public String generateId() {
this.id = this.worldObj.provider.dimensionId + ":" + this.xCoord + ":" + this.yCoord + ":" + this.zCoord;
if(this.worldObj != null && TileNode.locations != null) {
ArrayList t = new ArrayList();
t.add(Integer.valueOf(this.worldObj.provider.dimensionId));
t.add(Integer.valueOf(this.xCoord));
t.add(Integer.valueOf(this.yCoord));
t.add(Integer.valueOf(this.zCoord));
TileNode.locations.put(this.id, t);
this.locationRegistered = true;
}
return this.id;
}
public int getRange() {
return 16;
}
public boolean isSource() {
return true;
}
public int consumeVis(Aspect aspect, int vis) {
return vis;
}
public byte getAttunement() {
return (byte)-1;
}
}