Skip to content

Commit

Permalink
configurable autoplace
Browse files Browse the repository at this point in the history
  • Loading branch information
Glease committed Aug 23, 2022
1 parent b2624a3 commit a12722c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public enum ConfigurationHandler {
private Configuration config;
private int maxCoexistingHologram;
private boolean removeCollidingHologram;
private int autoPlaceBudget;
private int autoPlaceInterval;

ConfigurationHandler() {
FMLCommonHandler.instance().bus().register(this);
Expand Down Expand Up @@ -50,6 +52,25 @@ private void loadConfig() {
"client.hologram",
true,
"An attempt will be made to remove an existing hologram if it collides with a new hologram.");
autoPlaceBudget = config.getInt(
"autoPlaceBudget",
"common.hologram",
25,
1,
200,
"Max number of elements can be placed in one round of auto place.\n"
+ "As expected, server side settings will overrides client settings.\n"
+ "Certain larger multi might increase these values beyond this configured value.");
autoPlaceInterval = config.getInt(
"autoPlaceInterval",
"common.hologram",
300,
0,
20000,
"Unit: milisecond. Minimal interval between two auto place round.\n"
+ "As expected, server side settings will overrides client settings.\n"
+ "Note this relates to the wall clock, not in game ticks.\n"
+ "Value smaller than default is likely to be perceived as no minimal interval whatsoever.");

if (config.hasChanged()) {
config.save();
Expand All @@ -71,6 +92,14 @@ public boolean isRemoveCollidingHologram() {
return removeCollidingHologram;
}

public int getAutoPlaceBudget() {
return autoPlaceBudget;
}

public int getAutoPlaceInterval() {
return autoPlaceInterval;
}

Configuration getConfig() {
return config;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.gtnewhorizon.structurelib.alignment.constructable;

import static com.gtnewhorizon.structurelib.util.MiscUtils.getTagKeys;
import static net.minecraftforge.common.util.Constants.NBT.TAG_COMPOUND;

import com.gtnewhorizon.structurelib.StructureLib;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.stream.Stream;

import com.gtnewhorizon.structurelib.StructureLib;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.Constants.NBT;
import org.apache.commons.lang3.tuple.ImmutablePair;


import static com.gtnewhorizon.structurelib.util.MiscUtils.getTagKeys;
import static net.minecraftforge.common.util.Constants.NBT.TAG_COMPOUND;

public class ChannelDataAccessor {
private static final String SECONDARY_HINT_TAG = "channels";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.gtnewhorizon.structurelib.alignment.constructable;

import com.gtnewhorizon.structurelib.ConfigurationHandler;
import com.gtnewhorizon.structurelib.StructureLib;
import com.gtnewhorizon.structurelib.StructureLibAPI;
import com.gtnewhorizon.structurelib.alignment.IAlignment;
import com.gtnewhorizon.structurelib.alignment.enumerable.ExtendedFacing;
import com.gtnewhorizon.structurelib.structure.IItemSource;
import java.util.WeakHashMap;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.world.World;
Expand All @@ -17,9 +18,8 @@

public class ConstructableUtility {

// TODO make these configurable and expose this to external world
public static final int COOLDOWN = 5;
private static final int BUDGET = 25;
private static final WeakHashMap<EntityPlayerMP, Long> lastUse = new WeakHashMap<>();
private static long clientSideLastUse = 0;

private ConstructableUtility() {}

Expand All @@ -41,10 +41,11 @@ private static boolean handle0(
// not sneaking - client side will generate hologram. nothing to do on server side
if (!aPlayer.isSneaking()) return true;

long timePast = StructureLib.getOverworldTime() - getLastUseTick(aStack);
if (timePast < COOLDOWN) {
long timePast = System.currentTimeMillis() - getLastUseMilis(aPlayer);
if (timePast < ConfigurationHandler.INSTANCE.getAutoPlaceInterval()) {
aPlayer.addChatComponentMessage(new ChatComponentTranslation(
"item.structurelib.constructableTrigger.too_fast", COOLDOWN - timePast));
"item.structurelib.constructableTrigger.too_fast",
ConfigurationHandler.INSTANCE.getAutoPlaceInterval() - timePast));
return true;
}
} else if (!StructureLib.isCurrentPlayer(aPlayer)) {
Expand Down Expand Up @@ -74,13 +75,17 @@ else if (tTileEntity instanceof IConstructable) {
constructable.construct(aStack, false);
} else if (constructable instanceof ISurvivalConstructable) {
int built = ((ISurvivalConstructable) constructable)
.survivalConstruct(aStack, BUDGET, IItemSource.fromPlayer(playerMP), playerMP);
.survivalConstruct(
aStack,
ConfigurationHandler.INSTANCE.getAutoPlaceBudget(),
IItemSource.fromPlayer(playerMP),
playerMP);
if (built > 0) {
playerMP.addChatMessage(new ChatComponentTranslation("structurelib.autoplace.built_stat", built));
} else if (built == -1) {
playerMP.addChatMessage(new ChatComponentTranslation("structurelib.autoplace.complete"));
}
setLastUseTickToStackTag(aStack);
setLastUseMilis(aPlayer);
} else {
playerMP.addChatMessage(new ChatComponentTranslation("structurelib.autoplace.error.not_enabled"));
}
Expand All @@ -89,19 +94,23 @@ else if (tTileEntity instanceof IConstructable) {
// client side
// particles and text
constructable.construct(aStack, true);
if (getLastUseTick(aStack) == 0)
if (System.currentTimeMillis() - getLastUseMilis(aPlayer) >= 300)
StructureLib.addClientSideChatMessages(constructable.getStructureDescription(aStack));
setLastUseMilis(aPlayer);
return false;
}

private static long getLastUseTick(ItemStack aStack) {
return aStack.hasTagCompound() ? aStack.getTagCompound().getLong("LastUse") : 0;
private static void setLastUseMilis(EntityPlayer aPlayer) {
if (!(aPlayer instanceof EntityPlayerMP))
// assume client side
clientSideLastUse = System.currentTimeMillis();
else lastUse.put((EntityPlayerMP) aPlayer, System.currentTimeMillis());
}

private static void setLastUseTickToStackTag(ItemStack aStack) {
NBTTagCompound tag = aStack.stackTagCompound;
if (tag == null) tag = aStack.stackTagCompound = new NBTTagCompound();
// here we force use the overworld tick time, in case the current world is over
tag.setLong("LastUse", StructureLib.getOverworldTime());
private static long getLastUseMilis(EntityPlayer aPlayer) {
if (!(aPlayer instanceof EntityPlayerMP))
// assume client side
return clientSideLastUse;
return lastUse.getOrDefault(aPlayer, 0L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import com.gtnewhorizon.structurelib.alignment.constructable.ChannelDataAccessor;
import com.gtnewhorizon.structurelib.alignment.constructable.ConstructableUtility;
import java.util.List;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import org.lwjgl.input.Keyboard;

Expand Down Expand Up @@ -75,20 +73,4 @@ public void addInformation(ItemStack aStack, EntityPlayer ep, List aList, boolea
aList.add(translateToLocal("item.structurelib.constructableTrigger.desc.5"));
}
}

@Override
public void onUpdate(
ItemStack p_77663_1_, World p_77663_2_, Entity p_77663_3_, int p_77663_4_, boolean p_77663_5_) {
// remove LastUse tags if it times out,
// so it can stack with other trigger item stacks
// don't remove it too soon though, to prevent chat spam
// this might be perceived as a hack, but ok this is simple enough to implement
if (p_77663_1_.hasTagCompound()
&& p_77663_1_.getTagCompound().getLong("LastUse") + ConstructableUtility.COOLDOWN * 2
< StructureLib.getOverworldTime()) {
NBTTagCompound tag = p_77663_1_.getTagCompound();
tag.removeTag("LastUse");
if (tag.hasNoTags()) p_77663_1_.setTagCompound(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.gtnewhorizon.structurelib.structure;

import com.gtnewhorizon.structurelib.StructureLibAPI;
import com.gtnewhorizon.structurelib.alignment.constructable.ConstructableUtility;
import com.gtnewhorizon.structurelib.structure.IStructureElement.PlaceResult;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/structurelib/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ item.structurelib.constructableTrigger.desc.3=(Sneak use on controller to build)
item.structurelib.constructableTrigger.desc.4=Quantity of schematics in item stack affects tier/mode/type
item.structurelib.constructableTrigger.desc.5=Hold §l[LSHIFT]§r to display channel data
item.structurelib.constructableTrigger.desc.lshift.0=Contain %d sub channel.
item.structurelib.constructableTrigger.too_fast=Please wait %d ticks before another autoplace...
item.structurelib.constructableTrigger.too_fast=Please wait %d milliseconds before another autoplace...
item.structurelib.constructableTrigger.gui.key=Key:
item.structurelib.constructableTrigger.gui.value=Value:
item.structurelib.constructableTrigger.gui.add=Add
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/structurelib/lang/zh_CN.lang
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ item.structurelib.constructableTrigger.desc.3= (潜行右键可以直接完成
item.structurelib.constructableTrigger.desc.4=此物品堆叠数量会影响显示的机器等级/模式/类型
item.structurelib.constructableTrigger.desc.5=按下 §l[左SHIFT]§r 以显示子信道
item.structurelib.constructableTrigger.desc.lshift.0=包含 %d 个子信道
item.structurelib.constructableTrigger.too_fast=请在下一次自动搭建前等待%dtick...
item.structurelib.constructableTrigger.too_fast=请在下一次自动搭建前等待%d毫秒...
item.structurelib.constructableTrigger.gui.key=信道名:
item.structurelib.constructableTrigger.gui.value=值:
item.structurelib.constructableTrigger.gui.add=添加
Expand Down

0 comments on commit a12722c

Please sign in to comment.