Skip to content

Commit c22023b

Browse files
committed
Add field destabilization handling
1 parent f10bb5f commit c22023b

File tree

4 files changed

+82
-12
lines changed

4 files changed

+82
-12
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package dev.compactmods.crafting.api;
2+
3+
public enum FieldDestabilizeHandling {
4+
DESTROY_ALL,
5+
DESTROY_CATALYST,
6+
DESTROY_BLOCKS,
7+
RESTORE_ALL,
8+
RESTORE_CATALYST,
9+
RESTORE_BLOCKS
10+
}

src/main/java/dev/compactmods/crafting/field/MiniaturizationField.java

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import net.minecraft.nbt.CompoundNBT;
2727
import net.minecraft.nbt.NBTUtil;
2828
import net.minecraft.particles.ParticleTypes;
29+
import net.minecraft.util.Direction;
2930
import net.minecraft.util.ResourceLocation;
3031
import net.minecraft.util.math.AxisAlignedBB;
3132
import net.minecraft.util.math.BlockPos;
@@ -274,7 +275,7 @@ public void doRecipeScan() {
274275
// RECIPE BEGIN
275276
// ===========================================================================================================
276277

277-
AxisAlignedBB filledBounds = getFilledBounds();
278+
AxisAlignedBB filledBounds = getFilledBounds();
278279

279280
/*
280281
* Dry run - we have the data from the field on what's filled and how large
@@ -403,7 +404,7 @@ public CompoundNBT serverData() {
403404
nbt.putInt("progress", craftingProgress);
404405
}
405406

406-
if(matchedBlocks != null) {
407+
if (matchedBlocks != null) {
407408
nbt.put("matchedBlocks", matchedBlocks.save(new CompoundNBT()));
408409
}
409410

@@ -420,7 +421,7 @@ public void loadServerData(CompoundNBT nbt) {
420421
this.craftingProgress = nbt.getInt("progress");
421422
}
422423

423-
if(nbt.contains("matchedBlocks")) {
424+
if (nbt.contains("matchedBlocks")) {
424425
Template t = new Template();
425426
t.load(nbt.getCompound("matchedBlocks"));
426427
this.matchedBlocks = t;
@@ -442,12 +443,53 @@ public void setRecipe(ResourceLocation id) {
442443

443444
@Override
444445
public void handleProjectorBroken() {
445-
if(craftingState == EnumCraftingState.NOT_MATCHED || matchedBlocks == null)
446+
if (craftingState != EnumCraftingState.CRAFTING || matchedBlocks == null)
446447
return;
447448

448-
AxisAlignedBB bounds = getBounds();
449-
matchedBlocks.placeInWorld((IServerWorld) level, new BlockPos(bounds.minX, bounds.minY, bounds.minZ),
450-
new PlacementSettings(), level.random);
449+
if (level.isClientSide) return;
450+
451+
boolean restoreBlocks = false;
452+
boolean restoreCatalyst = false;
453+
switch (ServerConfig.DESTABILIZE_HANDLING) {
454+
case RESTORE_ALL:
455+
restoreBlocks = true;
456+
restoreCatalyst = true;
457+
break;
458+
459+
case RESTORE_BLOCKS:
460+
restoreBlocks = true;
461+
break;
462+
463+
case RESTORE_CATALYST:
464+
restoreCatalyst = true;
465+
break;
466+
467+
case DESTROY_ALL:
468+
break;
469+
470+
case DESTROY_BLOCKS:
471+
restoreCatalyst = true;
472+
break;
473+
474+
case DESTROY_CATALYST:
475+
restoreBlocks = true;
476+
break;
477+
}
478+
479+
if (restoreBlocks) {
480+
AxisAlignedBB bounds = getBounds();
481+
matchedBlocks.placeInWorld((IServerWorld) level, new BlockPos(bounds.minX, bounds.minY, bounds.minZ),
482+
new PlacementSettings(), level.random);
483+
}
484+
485+
final ItemStack catalyst = currentRecipe.getCatalyst();
486+
if (restoreCatalyst && !catalyst.isEmpty()) {
487+
final BlockPos northLoc = size.getProjectorLocationForDirection(center, Direction.NORTH);
488+
final ItemEntity ie = new ItemEntity(level, northLoc.getX(), center.getY() + 1.5f, northLoc.getZ(), catalyst);
489+
// ie.setNoGravity(true);
490+
491+
level.addFreshEntity(ie);
492+
}
451493
}
452494

453495
@Override

src/main/java/dev/compactmods/crafting/projector/FieldProjectorTile.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import javax.annotation.Nonnull;
44
import javax.annotation.Nullable;
5-
import dev.compactmods.crafting.CompactCrafting;
65
import dev.compactmods.crafting.Registration;
76
import dev.compactmods.crafting.api.field.IActiveWorldFields;
87
import dev.compactmods.crafting.api.field.IMiniaturizationField;
@@ -79,10 +78,6 @@ public LazyOptional<IMiniaturizationField> getField() {
7978
}
8079

8180
public void setFieldRef(LazyOptional<IMiniaturizationField> fieldRef) {
82-
if (level.isClientSide) {
83-
CompactCrafting.LOGGER.debug("Setting field reference in tile");
84-
CompactCrafting.LOGGER.debug(level.isClientSide ? "c" : "s", new Throwable());
85-
}
8681
this.fieldCap = fieldRef;
8782
setChanged();
8883
}

src/main/java/dev/compactmods/crafting/server/ServerConfig.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package dev.compactmods.crafting.server;
22

3+
import com.electronwill.nightconfig.core.EnumGetMethod;
4+
import dev.compactmods.crafting.CompactCrafting;
5+
import dev.compactmods.crafting.api.FieldDestabilizeHandling;
36
import net.minecraftforge.common.ForgeConfigSpec;
7+
import net.minecraftforge.eventbus.api.SubscribeEvent;
8+
import net.minecraftforge.fml.common.Mod;
9+
import net.minecraftforge.fml.config.ModConfig;
410

11+
@Mod.EventBusSubscriber(modid = CompactCrafting.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
512
public class ServerConfig {
613

714
public static ForgeConfigSpec CONFIG;
@@ -21,6 +28,9 @@ public class ServerConfig {
2128
*/
2229
public static ForgeConfigSpec.BooleanValue RECIPE_MATCHING;
2330

31+
private static ForgeConfigSpec.EnumValue<FieldDestabilizeHandling> FIELD_DESTABILIZE_HANDLING;
32+
public static FieldDestabilizeHandling DESTABILIZE_HANDLING = FieldDestabilizeHandling.RESTORE_ALL;
33+
2434
static {
2535
generateConfig();
2636
}
@@ -46,6 +56,19 @@ private static void generateConfig() {
4656

4757
builder.pop();
4858

59+
builder.comment("Field Settings").push("field");
60+
61+
FIELD_DESTABILIZE_HANDLING = builder
62+
.comment("Changes how the field handles a destabilization event (such as a projector breaking mid-craft)")
63+
.defineEnum("destabilizeHandling", FieldDestabilizeHandling.RESTORE_ALL, EnumGetMethod.NAME_IGNORECASE);
64+
65+
builder.pop();
66+
4967
CONFIG = builder.build();
5068
}
69+
70+
@SubscribeEvent
71+
public static void onConfigEvent(final ModConfig.ModConfigEvent configEvent) {
72+
ServerConfig.DESTABILIZE_HANDLING = ServerConfig.FIELD_DESTABILIZE_HANDLING.get();
73+
}
5174
}

0 commit comments

Comments
 (0)