Skip to content

Commit

Permalink
Imporved Drone Center (GTNewHorizons#2591)
Browse files Browse the repository at this point in the history
* Add plascrete block as an exception

* Spotless

* Add sort button

* rebalance range

* small fix

* Add a localized button

* Rename and add comment

* Spotless

(cherry picked from commit 4df537a)
  • Loading branch information
RealSilverMoon authored and Dream-Master committed May 20, 2024
1 parent ff5673e commit 888315d
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import net.minecraftforge.common.DimensionManager;

import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.interfaces.tileentity.IHasWorldObjectAndCoords;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_MultiBlockBase;
import gregtech.api.util.GT_LanguageManager;
import gregtech.api.util.GT_Util;
import gregtech.api.util.GT_Utility;

public class DroneConnection {

String customName;
String unlocalizedName;
GT_MetaTileEntity_MultiBlockBase machine;
ItemStack machineItem;
ChunkCoordinates machineCoord;
Expand All @@ -37,6 +39,7 @@ public DroneConnection(GT_MetaTileEntity_MultiBlockBase machine, GT_MetaTileEnti
.getCoords();
this.world = centre.getBaseMetaTileEntity()
.getWorld();
unlocalizedName = machine.mName;
customName = Optional.ofNullable(centre.tempNameList.remove(machineCoord.toString()))
.orElse(machine.getLocalName());
}
Expand All @@ -61,6 +64,7 @@ public DroneConnection(NBTTagCompound aNBT) {
centreTag.getInteger("z"));
this.centre = (GT_MetaTileEntity_DroneCentre) getLoadedGT_BaseMachineAt(centreCoord, world, true);
this.customName = aNBT.getString("name");
this.unlocalizedName = aNBT.getString("unlocalizedName");
}

public GT_MetaTileEntity_MultiBlockBase getMachine() {
Expand All @@ -79,25 +83,42 @@ public boolean reCheckConnection() {
return isValid();
}

public String getCustomName() {
public String getCustomName(boolean localized) {
if (localized) return GT_LanguageManager.getTranslation("gt.blockmachines." + unlocalizedName + ".name");
return customName;
}

public float getDistanceSquared() {
return centreCoord.getDistanceSquaredToChunkCoordinates(machineCoord);
}

public void setCustomName(String name) {
customName = name;
}

public boolean isMachineShutdown() {
return machine != null && machine.shouldDisplayShutDownReason()
&& !machine.getBaseMetaTileEntity()
.isActive()
&& GT_Utility.isStringValid(
machine.getBaseMetaTileEntity()
.getLastShutDownReason()
.getDisplayString())
&& machine.getBaseMetaTileEntity()
.wasShutdown();
}

public NBTTagCompound transConnectionToNBT() {
NBTTagCompound aNBT = new NBTTagCompound();
if (!this.isValid()) return aNBT;
aNBT.setTag("machine", transGT_BaseMachineToNBT(machine));
aNBT.setTag("centre", transGT_BaseMachineToNBT(centre));
aNBT.setTag("machine", transCoordsToNBT(machineCoord));
aNBT.setTag("centre", transCoordsToNBT(centreCoord));
aNBT.setTag("item", machineItem.writeToNBT(new NBTTagCompound()));
aNBT.setInteger(
"worldID",
machine.getBaseMetaTileEntity()
centre.getBaseMetaTileEntity()
.getWorld().provider.dimensionId);
aNBT.setString("name", getCustomName());
aNBT.setString("name", getCustomName(false));
aNBT.setString("unlocalizedName", unlocalizedName);
return aNBT;
}

Expand All @@ -108,12 +129,11 @@ public GT_MetaTileEntity_MultiBlockBase getLoadedGT_BaseMachineAt(ChunkCoordinat
return (GT_MetaTileEntity_MultiBlockBase) ((IGregTechTileEntity) te).getMetaTileEntity();
}

private NBTTagCompound transGT_BaseMachineToNBT(GT_MetaTileEntity_MultiBlockBase machine) {
IHasWorldObjectAndCoords baseCoord = machine.getBaseMetaTileEntity();
private NBTTagCompound transCoordsToNBT(ChunkCoordinates coord) {
NBTTagCompound tag = new NBTTagCompound();
tag.setInteger("x", baseCoord.getXCoord());
tag.setInteger("y", baseCoord.getYCoord());
tag.setInteger("z", baseCoord.getZCoord());
tag.setInteger("x", coord.posX);
tag.setInteger("y", coord.posY);
tag.setInteger("z", coord.posZ);
return tag;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import static gregtech.api.util.GT_StructureUtility.buildHatchAdder;

import java.io.IOException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.item.EntityItem;
Expand Down Expand Up @@ -98,7 +101,9 @@ public class GT_MetaTileEntity_DroneCentre extends
private int buttonID;
private String searchFilter = "";
private boolean useRender = true;
private final List<DroneConnection> connectionList = new ArrayList<>();
private boolean showLocalizedName = false;
private String sort = "distance";
private List<DroneConnection> connectionList = new ArrayList<>();
public HashMap<String, String> tempNameList = new HashMap<>();
// Save centre by dimID
private static final HashMultimap<Integer, GT_MetaTileEntity_DroneCentre> droneMap = HashMultimap.create();
Expand Down Expand Up @@ -178,7 +183,7 @@ protected GT_Multiblock_Tooltip_Builder createTooltip() {
.addInfo("Monitors multiblock machines in range.")
.addInfo("Replace maintenance hatch on other multi with drone downlink module.")
.addInfo("Provides maintenance, power control, monitoring and etc.")
.addInfo("Range is determined by drone tier: T1-32, T2-128, T3-512")
.addInfo("Range is determined by drone tier: T1-128, T2-512, T3-4096")
.addInfo("Place drones in input bus; only one needed to operate.")
.addInfo("Automatically upgrade based on the drone level in the input bus.")
.addInfo("There is a chance per second that the drone will crash.")
Expand Down Expand Up @@ -282,6 +287,7 @@ public void loadNBTData(NBTTagCompound aNBT) {
super.loadNBTData(aNBT);
droneLevel = aNBT.getInteger("drone");
useRender = aNBT.getBoolean("useRender");
sort = aNBT.getString("sort");
NBTTagCompound nameList = aNBT.getCompoundTag("conList");
for (String s : nameList.func_150296_c()) {
tempNameList.put(s, nameList.getString(s));
Expand All @@ -293,9 +299,10 @@ public void saveNBTData(NBTTagCompound aNBT) {
super.saveNBTData(aNBT);
aNBT.setInteger("drone", droneLevel);
aNBT.setBoolean("useRender", useRender);
aNBT.setString("sort", sort);
NBTTagCompound conList = new NBTTagCompound();
for (DroneConnection con : connectionList) {
if (!Objects.equals(con.customName, con.machine.getLocalName()))
if (!con.customName.equals(con.machine.getLocalName()))
conList.setString(con.machineCoord.toString(), con.customName);
}
aNBT.setTag("conList", conList);
Expand Down Expand Up @@ -390,9 +397,9 @@ public List<DroneConnection> getConnectionList() {

public int getRange() {
return switch (droneLevel) {
case 1 -> 32;
case 2 -> 128;
case 3 -> 512;
case 1 -> 128;
case 2 -> 512;
case 3 -> 4096;
default -> 0;
};
}
Expand Down Expand Up @@ -611,8 +618,87 @@ public void readOnServer(int id, PacketBuffer buf) {
.setFocusOnGuiOpen(false)
.setBackground(GT_UITextures.BACKGROUND_TEXT_FIELD_LIGHT_GRAY.withOffset(-1, -1, 2, 2))
.addTooltip(StatCollector.translateToLocal("GT5U.gui.text.drone_search"))
.setPos(10, 30)
.setSize(240, 16));
.setPos(50, 30)
.setSize(200, 16))
// Sort button
.widget(new ButtonWidget() {

@Override
public ClickResult onClick(int buttonId, boolean doubleClick) {
ClickResult result = super.onClick(buttonId, doubleClick);
syncToServer(2, buffer -> {});
return result;
}

@Override
public void readOnServer(int id, PacketBuffer buf) {
switch (id) {
case 1 -> super.readOnServer(id, buf);
case 2 -> {
getContext().closeWindow(MACHINE_LIST_WINDOW_ID);
getContext().openSyncedWindow(MACHINE_LIST_WINDOW_ID);
}
}
}
}.setOnClick((clickData, widget) -> {
switch (sort) {
case "name" -> sort = "distance";
case "distance" -> sort = "error";
case "error" -> sort = "name";
}
})
.addTooltip(StatCollector.translateToLocal("GT5U.gui.button.drone_" + sort))
.setBackground(
() -> new IDrawable[] { GT_UITextures.BUTTON_STANDARD, GT_UITextures.OVERLAY_BUTTON_SORTING_MODE })
.setPos(10, 30)
.setSize(16, 16))
.widget(new FakeSyncWidget.StringSyncer(() -> sort, var1 -> sort = var1))
// Localized Button
.widget(new ButtonWidget() {

@Override
public ClickResult onClick(int buttonId, boolean doubleClick) {
ClickResult result = super.onClick(buttonId, doubleClick);
syncToServer(2, buffer -> {});
return result;
}

@Override
public void readOnServer(int id, PacketBuffer buf) {
switch (id) {
case 1 -> super.readOnServer(id, buf);
case 2 -> {
getContext().closeWindow(MACHINE_LIST_WINDOW_ID);
getContext().openSyncedWindow(MACHINE_LIST_WINDOW_ID);
}
}
}
}.setOnClick((clickData, widget) -> showLocalizedName = !showLocalizedName)
.addTooltip(StatCollector.translateToLocal("GT5U.gui.button.drone_showLocalName"))
.setBackground(
() -> new IDrawable[] {
showLocalizedName ? GT_UITextures.BUTTON_STANDARD_PRESSED : GT_UITextures.BUTTON_STANDARD,
GT_UITextures.OVERLAY_BUTTON_CYCLIC })
.setPos(30, 30)
.setSize(16, 16));
// Sort first
switch (sort) {
case "name" -> connectionList = connectionList.stream()
.sorted(
(o1, o2) -> Collator.getInstance(Locale.UK)
.compare(o1.getCustomName(false), o2.getCustomName(false)))
.collect(Collectors.toList());
case "distance" -> connectionList = connectionList.stream()
.sorted(Comparator.comparing(DroneConnection::getDistanceSquared))
.collect(Collectors.toList());
case "error" -> connectionList = connectionList.stream()
.sorted(
Comparator.comparing(DroneConnection::isMachineShutdown)
.reversed()
.thenComparing(DroneConnection::getDistanceSquared))
.collect(Collectors.toList());
}

Scrollable MachineContainer = new Scrollable().setVerticalScroll();
int posY = 0;
for (int i = 0; i < connectionList.size(); i++) {
Expand Down Expand Up @@ -741,7 +827,7 @@ public void readOnServer(int id, PacketBuffer buf) {
row.widget(
new TextWidget(
connectionList.get(i)
.getCustomName()).setTextAlignment(Alignment.CenterLeft)
.getCustomName(showLocalizedName)).setTextAlignment(Alignment.CenterLeft)
.setPos(0, 4));
MachineContainer.widget(
row.setAlignment(MainAxisAlignment.SPACE_BETWEEN)
Expand Down Expand Up @@ -778,7 +864,7 @@ public void onDestroy() {
}
}.setGetter(
() -> connectionList.get(buttonID)
.getCustomName())
.getCustomName(false))
.setSetter(
var -> connectionList.get(buttonID)
.setCustomName(var))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
Expand Down Expand Up @@ -225,10 +226,14 @@ private GT_MetaTileEntity_MultiBlockBase tryFindCoreGTMultiBlock() {
final TileEntity tTileEntity;
final boolean isMachineBlock;
tTileEntity = world.getTileEntity(aCoords.posX, aCoords.posY, aCoords.posZ);
isMachineBlock = GregTech_API.isMachineBlock(
world.getBlock(aCoords.posX, aCoords.posY, aCoords.posZ),
world.getBlockMetadata(aCoords.posX, aCoords.posY, aCoords.posZ));

Block block = world.getBlock(aCoords.posX, aCoords.posY, aCoords.posZ);
// Plascrete block isn't registered as machineBlock, therefore we have to check it manually so that drone
// can work with cleanroom.
// Todo: loading cleanroom's config for other blocks
isMachineBlock = GregTech_API
.isMachineBlock(block, world.getBlockMetadata(aCoords.posX, aCoords.posY, aCoords.posZ))
|| (block == GregTech_API.sBlockReinforced
&& world.getBlockMetadata(aCoords.posX, aCoords.posY, aCoords.posZ) == 2);
// See if the block itself is MultiBlock, also the one we need.
if (tTileEntity instanceof IGregTechTileEntity te
&& te.getMetaTileEntity() instanceof GT_MetaTileEntity_MultiBlockBase mte)
Expand Down Expand Up @@ -289,7 +294,7 @@ public void addUIWidgets(ModularWindow.Builder builder, UIBuildContext buildCont
.setPos(0, 5)
.setSize(150, 8))
.widget(
new TextFieldWidget().setGetter(() -> connection == null ? "" : connection.getCustomName())
new TextFieldWidget().setGetter(() -> connection == null ? "" : connection.getCustomName(false))
.setSetter(var -> { if (connection != null) connection.setCustomName(var); })
.setTextAlignment(Alignment.CenterLeft)
.setTextColor(Color.WHITE.dark(1))
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/assets/gregtech/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ GT5U.gui.button.drone_outofrange=§4Machine is too far, drone control system not
GT5U.gui.button.drone_open_list=Open Machine List
GT5U.gui.button.drone_poweron_all=Turn ON ALL machines, no matter how remote
GT5U.gui.button.drone_poweroff_all=Turn OFF ALL machines, no matter how remote
GT5U.gui.button.drone_name=Sort by §3name
GT5U.gui.button.drone_distance=Sort by §3distance
GT5U.gui.button.drone_error=Sort by §3shutdown status
GT5U.gui.button.drone_showLocalName=Show localized name
GT5U.gui.text.success=§aProcessing recipe
GT5U.gui.text.generating=§aGenerating power
GT5U.gui.text.no_recipe=§7No valid recipe found
Expand Down

0 comments on commit 888315d

Please sign in to comment.