Skip to content

Commit d1d6fbe

Browse files
[added] support for ComputerCraft
[edited] patch console spam with advanced input mode
1 parent 49ee0ae commit d1d6fbe

17 files changed

+492
-43
lines changed

build.gradle

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty '
2727
minecraft {
2828
// The mappings can be changed at any time and must be in the following format.
2929
// Channel: Version:
30-
// official MCVersion Official field/method names from Mojang mapping files
31-
// parchment YYYY.MM.DD-MCVersion Open community-sourced parameter names and javadocs layered on top of official
30+
// official {minecraft_version} Official field/method names from Mojang mapping files
31+
// parchment YYYY.MM.DD-{minecraft_version} Open community-sourced parameter names and javadocs layered on top of official
3232
//
3333
// You must be aware of the Mojang license when using the 'official' or 'parchment' mappings.
3434
// See more information here: https://github.com/MinecraftForge/MCPConfig/blob/master/Mojang.md
@@ -186,7 +186,10 @@ dependencies {
186186
compileOnly fg.deobf("mezz.jei:jei-${jei_mc_version}-common-api:${jei_version}")
187187
compileOnly fg.deobf("mezz.jei:jei-${jei_mc_version}-forge-api:${jei_version}")
188188
runtimeOnly fg.deobf("mezz.jei:jei-${jei_mc_version}-forge:${jei_version}")
189-
189+
190+
// CC TWEAKED COMPAT
191+
compileOnly fg.deobf("curse.maven:cc-tweaked-${cc_tweaked_version}")
192+
190193
//MIXINS
191194
if (System.getProperty('idea.sync.active') != 'true') {
192195
annotationProcessor "org.spongepowered:mixin:${mixin_version}:processor"

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ registrate_version = MC1.19-1.1.5
2828

2929
create_range = [0.5.1.a,)
3030

31+
cc_tweaked_version = 282001:4630521
32+
3133
# GitHub information
3234
github_project = getItemFromBlock/Create-Tweaked-Controllers

src/main/java/com/getitemfromblock/create_tweaked_controllers/CreateTweakedControllers.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.getitemfromblock.create_tweaked_controllers;
22

33
import com.getitemfromblock.create_tweaked_controllers.block.ModBlocks;
4+
import com.getitemfromblock.create_tweaked_controllers.compat.ComputerCraft.ModComputerCraftProxy;
45
import com.getitemfromblock.create_tweaked_controllers.config.ModConfigs;
56
import com.getitemfromblock.create_tweaked_controllers.gui.ModMenuTypes;
67
import com.getitemfromblock.create_tweaked_controllers.item.ModItems;
@@ -47,6 +48,7 @@ public CreateTweakedControllers()
4748
ModMenuTypes.register();
4849
ModConfigs.register(modLoadingContext);
4950
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ModClientStuff.onConstructor(eventBus, forgeEventBus));
51+
ModComputerCraftProxy.register();
5052
}
5153

5254
public static void init(final FMLCommonSetupEvent event)

src/main/java/com/getitemfromblock/create_tweaked_controllers/ModClientStuff.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.getitemfromblock.create_tweaked_controllers;
22

3-
import com.getitemfromblock.create_tweaked_controllers.compat.ControllerHandler;
3+
import com.getitemfromblock.create_tweaked_controllers.compat.Controllable.ControllerHandler;
44
import com.getitemfromblock.create_tweaked_controllers.input.MouseCursorHandler;
55

66
import net.minecraftforge.common.MinecraftForge;

src/main/java/com/getitemfromblock/create_tweaked_controllers/block/TweakedLecternControllerBlockEntity.java

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
import java.util.List;
44
import java.util.UUID;
55

6+
import javax.annotation.Nullable;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
10+
import com.getitemfromblock.create_tweaked_controllers.compat.ComputerCraft.ModComputerCraftProxy;
11+
import com.getitemfromblock.create_tweaked_controllers.compat.ComputerCraft.TweakedLecternPeripheral;
12+
import com.getitemfromblock.create_tweaked_controllers.controller.ControllerRedstoneOutput;
613
import com.getitemfromblock.create_tweaked_controllers.controller.TweakedLinkedControllerClientHandler;
714
import com.simibubi.create.AllSoundEvents;
15+
import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
816
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
917
import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour;
1018

@@ -19,36 +27,60 @@
1927
import net.minecraft.world.entity.player.Player;
2028
import net.minecraft.world.item.ItemStack;
2129
import net.minecraft.world.level.Level;
30+
import net.minecraft.world.level.block.Blocks;
2231
import net.minecraft.world.level.block.entity.BlockEntityType;
2332
import net.minecraft.world.level.block.state.BlockState;
2433
import net.minecraft.world.phys.Vec3;
2534
import net.minecraftforge.api.distmarker.Dist;
2635
import net.minecraftforge.api.distmarker.OnlyIn;
2736
import net.minecraftforge.common.ForgeMod;
37+
import net.minecraftforge.common.capabilities.Capability;
38+
import net.minecraftforge.common.util.LazyOptional;
2839
import net.minecraftforge.fml.DistExecutor;
2940

3041
public class TweakedLecternControllerBlockEntity extends SmartBlockEntity
3142
{
3243

3344
private ItemStack controller;
45+
3446
private UUID user;
3547
private UUID prevUser; // used only on client
3648
private boolean deactivatedThisTick; // used only on server
49+
private boolean useFullPrecision = false;
50+
private ControllerRedstoneOutput output;
51+
private float[] axis;
52+
private TweakedLecternPeripheral peripheral = null;
53+
54+
public AbstractComputerBehaviour computerBehaviour;
3755

3856
public TweakedLecternControllerBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state)
3957
{
4058
super(type, pos, state);
59+
output = new ControllerRedstoneOutput();
60+
output.DecodeAxis(0);
61+
output.DecodeButtons((short)0);
62+
axis = new float[6];
63+
for (byte i = 0; i < 6; i++)
64+
{
65+
axis[i] = 0;
66+
}
4167
}
4268

4369
@Override
4470
public void addBehaviours(List<BlockEntityBehaviour> behaviours)
4571
{
72+
behaviours.add(computerBehaviour = ModComputerCraftProxy.behaviour(this));
4673
}
4774

4875
@Override
4976
protected void write(CompoundTag compound, boolean clientPacket)
5077
{
5178
super.write(compound, clientPacket);
79+
compound.putBoolean("UseFullPrecision", useFullPrecision);
80+
if (controller == null)
81+
{
82+
controller = new ItemStack(Blocks.AIR, 0);
83+
}
5284
compound.put("Controller", controller.save(new CompoundTag()));
5385
if (user != null)
5486
compound.putUUID("User", user);
@@ -58,23 +90,95 @@ protected void write(CompoundTag compound, boolean clientPacket)
5890
public void writeSafe(CompoundTag compound)
5991
{
6092
super.writeSafe(compound);
93+
compound.putBoolean("UseFullPrecision", useFullPrecision);
6194
compound.put("Controller", controller.save(new CompoundTag()));
6295
}
6396

6497
@Override
6598
protected void read(CompoundTag compound, boolean clientPacket)
6699
{
67100
super.read(compound, clientPacket);
101+
useFullPrecision = compound.getBoolean("UseFullPrecision");
68102
controller = ItemStack.of(compound.getCompound("Controller"));
69103
user = compound.hasUUID("User") ? compound.getUUID("User") : null;
70104
}
71105

106+
public void AssignPeripheral(TweakedLecternPeripheral p)
107+
{
108+
peripheral = p;
109+
}
110+
111+
public void ReceiveButtonStates(short value)
112+
{
113+
output.DecodeButtons(value);
114+
}
115+
116+
public boolean GetButton(int index)
117+
{
118+
return output.buttons[index];
119+
}
120+
121+
public void ReceiveAxisStates(int value)
122+
{
123+
output.DecodeAxis(value);
124+
}
125+
126+
public void ReceiveFullStates(float[] value)
127+
{
128+
for (byte i = 0; i < 6; i++)
129+
{
130+
axis[i] = value[i];
131+
}
132+
}
133+
134+
public float GetAxis(int index)
135+
{
136+
if (useFullPrecision)
137+
{
138+
return axis[index];
139+
}
140+
else
141+
{
142+
Byte input = output.axis[index];
143+
float result;
144+
if ((input & 0x10) != 0)
145+
{
146+
result = -(input & 0x0f) / 15.0f;
147+
}
148+
else
149+
{
150+
result = input / 15.0f;
151+
}
152+
return result;
153+
}
154+
}
155+
72156
public ItemStack getController()
73157
{
74158
return controller;
75159
}
76160

77-
public boolean hasUser() { return user != null; }
161+
public boolean hasUser()
162+
{
163+
return user != null;
164+
}
165+
166+
public UUID getUserUUID()
167+
{
168+
return user;
169+
}
170+
171+
public void SetFullPrecision(boolean value)
172+
{
173+
if (useFullPrecision == value) return;
174+
useFullPrecision = value;
175+
sendData();
176+
}
177+
178+
public boolean shouldUseFullPrecision()
179+
{
180+
return useFullPrecision;
181+
}
78182

79183
public boolean isUsedBy(Player player)
80184
{
@@ -95,13 +199,21 @@ public void tryStopUsing(Player player)
95199

96200
private void startUsing(Player player)
97201
{
202+
if (peripheral != null)
203+
{
204+
peripheral.NotifyUseEvent(true, player);
205+
}
98206
user = player.getUUID();
99207
player.getPersistentData().putBoolean("IsUsingLecternController", true);
100208
sendData();
101209
}
102210

103211
private void stopUsing(Player player)
104212
{
213+
if (peripheral != null)
214+
{
215+
peripheral.NotifyUseEvent(false, player);
216+
}
105217
user = null;
106218
if (player != null)
107219
player.getPersistentData().remove("IsUsingLecternController");
@@ -162,7 +274,8 @@ else if (prevUser == null && Minecraft.getInstance().player.getUUID().equals(use
162274
public void setController(ItemStack newController)
163275
{
164276
controller = newController;
165-
if (newController != null) {
277+
if (newController != null)
278+
{
166279
AllSoundEvents.CONTROLLER_PUT.playOnServer(level, worldPosition);
167280
}
168281
}
@@ -205,4 +318,13 @@ public static boolean playerInRange(Player player, Level world, BlockPos pos)
205318
return player.distanceToSqr(Vec3.atCenterOf(pos)) < reach*reach;
206319
}
207320

321+
@NotNull
322+
@Override
323+
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side)
324+
{
325+
if (computerBehaviour.isPeripheralCap(cap))
326+
return computerBehaviour.getPeripheralCapability();
327+
return super.getCapability(cap, side);
328+
}
329+
208330
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.getitemfromblock.create_tweaked_controllers.compat.ComputerCraft;
2+
3+
import com.getitemfromblock.create_tweaked_controllers.block.TweakedLecternControllerBlockEntity;
4+
import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
5+
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
6+
7+
import dan200.computercraft.api.peripheral.IPeripheral;
8+
import net.minecraftforge.common.capabilities.Capability;
9+
import net.minecraftforge.common.capabilities.CapabilityManager;
10+
import net.minecraftforge.common.capabilities.CapabilityToken;
11+
import net.minecraftforge.common.util.LazyOptional;
12+
import net.minecraftforge.common.util.NonNullSupplier;
13+
14+
public class ModComputerBehavior extends AbstractComputerBehaviour
15+
{
16+
17+
protected static final Capability<IPeripheral> PERIPHERAL_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {});
18+
LazyOptional<IPeripheral> peripheral;
19+
NonNullSupplier<IPeripheral> peripheralSupplier;
20+
21+
public ModComputerBehavior(SmartBlockEntity te)
22+
{
23+
super(te);
24+
this.peripheralSupplier = getPeripheralFor(te);
25+
}
26+
27+
public static NonNullSupplier<IPeripheral> getPeripheralFor(SmartBlockEntity be)
28+
{
29+
if (be instanceof TweakedLecternControllerBlockEntity tlcbe)
30+
return () -> new TweakedLecternPeripheral(tlcbe);
31+
32+
throw new IllegalArgumentException("No peripheral available for " + be.getType()
33+
.getRegistryName());
34+
}
35+
36+
@Override
37+
public <T> boolean isPeripheralCap(Capability<T> cap)
38+
{
39+
return cap == PERIPHERAL_CAPABILITY;
40+
}
41+
42+
@Override
43+
public <T> LazyOptional<T> getPeripheralCapability()
44+
{
45+
if (peripheral == null || !peripheral.isPresent())
46+
peripheral = LazyOptional.of(peripheralSupplier);
47+
return peripheral.cast();
48+
}
49+
50+
@Override
51+
public void removePeripheral()
52+
{
53+
if (peripheral != null)
54+
peripheral.invalidate();
55+
}
56+
57+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.getitemfromblock.create_tweaked_controllers.compat.ComputerCraft;
2+
3+
import java.util.function.Function;
4+
5+
import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
6+
import com.simibubi.create.compat.computercraft.FallbackComputerBehaviour;
7+
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
8+
9+
import net.minecraftforge.fml.ModList;
10+
11+
public class ModComputerCraftProxy
12+
{
13+
private static Function<SmartBlockEntity, ? extends AbstractComputerBehaviour> fallbackFactory;
14+
private static Function<SmartBlockEntity, ? extends AbstractComputerBehaviour> computerFactory;
15+
16+
public static void register()
17+
{
18+
fallbackFactory = FallbackComputerBehaviour::new;
19+
if (ModList.get().isLoaded("computercraft"))
20+
{
21+
registerWithDependency();
22+
}
23+
}
24+
25+
private static void registerWithDependency()
26+
{
27+
/* Comment if computercraft.implementation is not in the source set */
28+
computerFactory = ModComputerBehavior::new;
29+
}
30+
31+
public static AbstractComputerBehaviour behaviour(SmartBlockEntity sbe)
32+
{
33+
if (computerFactory == null)
34+
return fallbackFactory.apply(sbe);
35+
return computerFactory.apply(sbe);
36+
}
37+
}

0 commit comments

Comments
 (0)