PlayerEventState.java
3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.mumfrey.liteloader.core;
import java.lang.ref.WeakReference;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.MovingObjectPosition.MovingObjectType;
import com.mumfrey.liteloader.PlayerInteractionListener.MouseButton;
import com.mumfrey.liteloader.core.LiteLoaderEventBroker.InteractType;
import com.mumfrey.liteloader.util.EntityUtilities;
public class PlayerEventState implements IEventState
{
private static long MISS = new BlockPos(-1, -1, -1).toLong();
private WeakReference<EntityPlayerMP> playerRef;
private final LiteLoaderEventBroker<?, ?> broker;
private double traceDistance = 256.0;
private int suppressLeftTicks;
private int suppressRightTicks;
private boolean leftClick;
private boolean rightClick;
private MovingObjectPosition hit;
private String locale = "en_US";
public PlayerEventState(EntityPlayerMP player, LiteLoaderEventBroker<?, ?> broker)
{
this.playerRef = new WeakReference<EntityPlayerMP>(player);
this.broker = broker;
}
public void setTraceDistance(int renderDistance)
{
this.traceDistance = renderDistance * 16.0;
}
public double getTraceDistance()
{
return this.traceDistance;
}
public void setLocale(String lang)
{
if (lang.matches("^[a-z]{2}_[A-Z]{2}$"))
{
this.locale = lang;
}
}
public String getLocale()
{
return this.locale;
}
public EntityPlayerMP getPlayer()
{
return this.playerRef.get();
}
public void onSpawned()
{
}
@Override
public void onTick(MinecraftServer server)
{
if (this.leftClick && this.suppressLeftTicks == 0)
{
this.broker.onPlayerClickedAir(this.getPlayer(), MouseButton.LEFT, this.hit.getBlockPos(), this.hit.sideHit, this.hit.typeOfHit);
}
if (this.rightClick && this.suppressRightTicks == 0)
{
this.broker.onPlayerClickedAir(this.getPlayer(), MouseButton.RIGHT, this.hit.getBlockPos(), this.hit.sideHit, this.hit.typeOfHit);
}
if (this.suppressLeftTicks > 0) this.suppressLeftTicks--;
if (this.suppressRightTicks > 0) this.suppressRightTicks--;
this.leftClick = false;
this.rightClick = false;
}
public boolean onPlayerInteract(InteractType action, EntityPlayerMP player, BlockPos position, EnumFacing side)
{
this.hit = EntityUtilities.rayTraceFromEntity(player, this.traceDistance, 0.0F);
if (action == InteractType.LEFT_CLICK)
{
this.leftClick = true;
return true;
}
if (action == InteractType.RIGHT_CLICK)
{
this.rightClick = true;
return true;
}
if ((action == InteractType.LEFT_CLICK_BLOCK || action == InteractType.DIG_BLOCK_MAYBE) && this.suppressLeftTicks == 0)
{
this.suppressLeftTicks += 2;
return this.broker.onPlayerClickedBlock(player, MouseButton.LEFT, position, side);
}
if (action == InteractType.PLACE_BLOCK_MAYBE)
{
if (this.suppressRightTicks > 0)
{
return true;
}
if (position.toLong() == PlayerEventState.MISS)
{
MovingObjectPosition actualHit = EntityUtilities.rayTraceFromEntity(player, player.capabilities.isCreativeMode ? 5.0 : 4.5, 0.0F);
if (actualHit.typeOfHit == MovingObjectType.MISS)
{
this.rightClick = true;
return true;
}
}
this.suppressRightTicks++;
this.suppressLeftTicks++;
return this.broker.onPlayerClickedBlock(player, MouseButton.RIGHT, position, side);
}
return true;
}
}