Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give MTEs a owner on placement #2540

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public void onBlockPlacedBy(World worldIn, @NotNull BlockPos pos, @NotNull IBloc
}
}

metaTileEntity.onPlacement();
metaTileEntity.onPlacement(placer);
}
}

Expand Down
38 changes: 36 additions & 2 deletions src/main/java/gregtech/api/metatileentity/MetaTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.I18n;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
Expand Down Expand Up @@ -109,6 +110,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand Down Expand Up @@ -160,6 +162,8 @@ public abstract class MetaTileEntity implements ISyncedTileEntity, CoverHolder,
private int playSoundCooldown = 0;
private int lastTick = 0;

private UUID owner;

protected MetaTileEntity(@NotNull ResourceLocation metaTileEntityId) {
this.metaTileEntityId = metaTileEntityId;
this.registry = GregTechAPI.mteManager.getRegistry(metaTileEntityId.getNamespace());
Expand Down Expand Up @@ -506,6 +510,10 @@ public boolean onRightClick(EntityPlayer playerIn, EnumHand hand, EnumFacing fac
} else {
MetaTileEntityUIFactory.INSTANCE.openUI(getHolder(), (EntityPlayerMP) playerIn);
}

if (getOwner() == null) {
this.owner = playerIn.getUniqueID();
}
}
return true;
} else {
Expand Down Expand Up @@ -1293,6 +1301,9 @@ public NBTTagCompound writeToNBT(NBTTagCompound data) {
CoverSaveHandler.writeCoverNBT(data, this);

data.setBoolean(TAG_KEY_MUFFLED, muffled);

data.setUniqueId("Owner", owner);

return data;
}

Expand All @@ -1318,6 +1329,8 @@ public void readFromNBT(NBTTagCompound data) {

CoverSaveHandler.readCoverNBT(data, this, covers::put);
this.muffled = data.getBoolean(TAG_KEY_MUFFLED);

this.owner = data.getUniqueId("Owner");
}

@Override
Expand Down Expand Up @@ -1346,12 +1359,28 @@ public int getItemStackLimit(ItemStack stack) {
}

/**
* Called whenever a MetaTileEntity is placed in world by {@link Block#onBlockPlacedBy}
* Called whenever a MetaTileEntity is placed in world by {@link Block#onBlockPlacedBy},
* gives the MetaTileEntity an Owner by UUID
* <p>
* If placing an MTE with methods such as {@link World#setBlockState(BlockPos, IBlockState)},
* this should be manually called immediately afterwards
*/
public void onPlacement(@Nullable EntityLivingBase placer) {
if (placer instanceof EntityPlayer player) {
this.owner = player.getUniqueID();
}
}

/**
* Called whenever a MetaTileEntity is placed in world by {@link Block#onBlockPlacedBy},
* gives the MetaTileEntity an Owner of Null
* <p>
* If placing an MTE with methods such as {@link World#setBlockState(BlockPos, IBlockState)},
* this should be manually called immediately afterwards
*/
public void onPlacement() {}
public void onPlacement() {
this.owner = null;
}

/**
* Called from breakBlock right before meta tile entity destruction
Expand Down Expand Up @@ -1451,6 +1480,11 @@ public boolean getWitherProof() {
return false;
}

@Nullable
public UUID getOwner() {
Synthitic marked this conversation as resolved.
Show resolved Hide resolved
return owner;
}

public final void toggleMuffled() {
muffled = !muffled;
if (!getWorld().isRemote) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public void onChunkUnload() {
public boolean shouldRefresh(@NotNull World world, @NotNull BlockPos pos, IBlockState oldState,
IBlockState newState) {
return oldState.getBlock() != newState.getBlock(); // MetaTileEntityHolder should never refresh (until block
// changes)
// changes)
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
Expand Down Expand Up @@ -91,8 +92,8 @@ public MultiblockControllerBase(ResourceLocation metaTileEntityId) {
}

@Override
public void onPlacement() {
super.onPlacement();
public void onPlacement(EntityLivingBase placer) {
super.onPlacement(placer);
reinitializeStructurePattern();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/pattern/BlockPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public void autoBuild(EntityPlayer player, MultiblockControllerBase controllerBa
MetaTileEntity sampleMetaTileEntity = registry.getObjectById(found.getItemDamage());
if (sampleMetaTileEntity != null) {
MetaTileEntity metaTileEntity = igtte.setMetaTileEntity(sampleMetaTileEntity);
metaTileEntity.onPlacement();
metaTileEntity.onPlacement(player);
blocks.put(pos, metaTileEntity);
if (found.getTagCompound() != null) {
metaTileEntity.initFromItemStackData(found.getTagCompound());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

public class TricorderBehavior implements IItemBehaviour {

Expand Down Expand Up @@ -141,13 +142,29 @@ public List<ITextComponent> getScannerInfo(EntityPlayer player, World world, Blo

// name of the machine
list.add(new TextComponentTranslation("behavior.tricorder.block_name",
new TextComponentTranslation(LocalizationUtils.format(metaTileEntity.getMetaFullName()))
new TextComponentTranslation(metaTileEntity.getMetaFullName())
.setStyle(new Style().setColor(TextFormatting.BLUE)),
new TextComponentTranslation(TextFormattingUtil
.formatNumbers(
metaTileEntity.getRegistry().getIdByObjectName(metaTileEntity.metaTileEntityId)))
.setStyle(new Style().setColor(TextFormatting.BLUE))));

UUID owner = metaTileEntity.getOwner();
if (owner != null) {
EntityPlayer ownerEntity = metaTileEntity.getWorld().getPlayerEntityByUUID(owner);
if (ownerEntity != null) {
list.add(new TextComponentTranslation("behavior.tricorder.mte_owner",
Synthitic marked this conversation as resolved.
Show resolved Hide resolved
new TextComponentTranslation(ownerEntity.getName())
.setStyle(new Style().setColor(TextFormatting.AQUA))));
} else {
list.add(new TextComponentTranslation("behavior.tricorder.mte_owner_offline")
.setStyle(new Style().setColor(TextFormatting.YELLOW)));
}
} else {
list.add(new TextComponentTranslation("behavior.tricorder.mte_owner_null")
.setStyle(new Style().setColor(TextFormatting.RED)));
}

list.add(new TextComponentTranslation("behavior.tricorder.divider"));

// fluid tanks
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/assets/gregtech/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2466,6 +2466,9 @@ behavior.tricorder.debug_machine_invalid_null=invalid! MetaTileEntity == null!
behavior.tricorder.debug_cpu_load=Average CPU load of ~%sns over %s ticks with worst time of %sns.
behavior.tricorder.debug_cpu_load_seconds=This is %s seconds.
behavior.tricorder.debug_lag_count=Caused %s Lag Spike Warnings (anything taking longer than %sms) on the Server.
behavior.tricorder.mte_owner=Owner: %s
behavior.tricorder.mte_owner_offline=Owner is Offline or Offworld!
behavior.tricorder.mte_owner_null=This wasn't placed by a player!

metaitem.item_magnet.lv.name=Low Voltage Item Magnet
metaitem.item_magnet.hv.name=High Voltage Item Magnet
Expand Down