33import java .util .List ;
44import 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 ;
613import com .getitemfromblock .create_tweaked_controllers .controller .TweakedLinkedControllerClientHandler ;
714import com .simibubi .create .AllSoundEvents ;
15+ import com .simibubi .create .compat .computercraft .AbstractComputerBehaviour ;
816import com .simibubi .create .foundation .blockEntity .SmartBlockEntity ;
917import com .simibubi .create .foundation .blockEntity .behaviour .BlockEntityBehaviour ;
1018
1927import net .minecraft .world .entity .player .Player ;
2028import net .minecraft .world .item .ItemStack ;
2129import net .minecraft .world .level .Level ;
30+ import net .minecraft .world .level .block .Blocks ;
2231import net .minecraft .world .level .block .entity .BlockEntityType ;
2332import net .minecraft .world .level .block .state .BlockState ;
2433import net .minecraft .world .phys .Vec3 ;
2534import net .minecraftforge .api .distmarker .Dist ;
2635import net .minecraftforge .api .distmarker .OnlyIn ;
2736import net .minecraftforge .common .ForgeMod ;
37+ import net .minecraftforge .common .capabilities .Capability ;
38+ import net .minecraftforge .common .util .LazyOptional ;
2839import net .minecraftforge .fml .DistExecutor ;
2940
3041public 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}
0 commit comments